Class: Ukiryu::Validation::TypeConstraint

Inherits:
Constraint
  • Object
show all
Defined in:
lib/ukiryu/validation/constraints.rb

Overview

Validates type constraints using the Type module

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Constraint

#applies_to?

Constructor Details

#initialize(attribute_name, type, validation_options: {}) ⇒ TypeConstraint

Returns a new instance of TypeConstraint.

Parameters:

  • attribute_name (String, Symbol)

    the attribute name

  • type (Symbol, Hash)

    the type definition

  • validation_options (Hash) (defaults to: {})

    additional validation options



119
120
121
122
123
# File 'lib/ukiryu/validation/constraints.rb', line 119

def initialize(attribute_name, type, validation_options: {})
  @attribute_name = attribute_name
  @type = type
  @validation_options = validation_options
end

Instance Attribute Details

#attribute_nameObject (readonly)

Returns the value of attribute attribute_name.



114
115
116
# File 'lib/ukiryu/validation/constraints.rb', line 114

def attribute_name
  @attribute_name
end

#typeObject (readonly)

Returns the value of attribute type.



114
115
116
# File 'lib/ukiryu/validation/constraints.rb', line 114

def type
  @type
end

#validation_optionsObject (readonly)

Returns the value of attribute validation_options.



114
115
116
# File 'lib/ukiryu/validation/constraints.rb', line 114

def validation_options
  @validation_options
end

Instance Method Details

#validate(value, _context = {}) ⇒ Object

Raises:

  • (ValidationError)

    if value doesn’t match type



126
127
128
129
130
131
132
133
134
135
# File 'lib/ukiryu/validation/constraints.rb', line 126

def validate(value, _context = {})
  return if value.nil?

  begin
    Type.validate(value, @type, @validation_options)
  rescue Ukiryu::Errors::ValidationError => e
    raise ValidationIssue.new(@attribute_name, :type,
                              "#{@attribute_name}: #{e.message}")
  end
end