Class: Ukiryu::Validation::EnumConstraint
- Inherits:
-
Constraint
- Object
- Constraint
- Ukiryu::Validation::EnumConstraint
- Defined in:
- lib/ukiryu/validation/constraints.rb
Overview
Validates that values match one of the allowed values
Instance Attribute Summary collapse
-
#allowed_values ⇒ Object
readonly
Returns the value of attribute allowed_values.
-
#attribute_name ⇒ Object
readonly
Returns the value of attribute attribute_name.
Instance Method Summary collapse
-
#initialize(attribute_name, allowed_values:) ⇒ EnumConstraint
constructor
A new instance of EnumConstraint.
- #validate(value, _context = {}) ⇒ Object
Methods inherited from Constraint
Constructor Details
#initialize(attribute_name, allowed_values:) ⇒ EnumConstraint
Returns a new instance of EnumConstraint.
89 90 91 92 |
# File 'lib/ukiryu/validation/constraints.rb', line 89 def initialize(attribute_name, allowed_values:) @attribute_name = attribute_name @allowed_values = allowed_values end |
Instance Attribute Details
#allowed_values ⇒ Object (readonly)
Returns the value of attribute allowed_values.
85 86 87 |
# File 'lib/ukiryu/validation/constraints.rb', line 85 def allowed_values @allowed_values end |
#attribute_name ⇒ Object (readonly)
Returns the value of attribute attribute_name.
85 86 87 |
# File 'lib/ukiryu/validation/constraints.rb', line 85 def attribute_name @attribute_name end |
Instance Method Details
#validate(value, _context = {}) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ukiryu/validation/constraints.rb', line 95 def validate(value, _context = {}) return if value.nil? if value.is_a?(Array) invalid = value - @allowed_values unless invalid.empty? raise ValidationIssue.new(@attribute_name, :enum, "#{@attribute_name} contains invalid values: #{invalid.join(', ')}. " \ "Valid values: #{@allowed_values.join(', ')}") end elsif !@allowed_values.include?(value) raise ValidationIssue.new(@attribute_name, :enum, "#{@attribute_name} must be one of #{@allowed_values.join(', ')}, got #{value}") end end |