Class: Ukiryu::Validation::RangeConstraint
- Inherits:
-
Constraint
- Object
- Constraint
- Ukiryu::Validation::RangeConstraint
- Defined in:
- lib/ukiryu/validation/constraints.rb
Overview
Validates numeric or array length ranges
Instance Attribute Summary collapse
-
#attribute_name ⇒ Object
readonly
Returns the value of attribute attribute_name.
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
Instance Method Summary collapse
-
#initialize(attribute_name, min:, max:) ⇒ RangeConstraint
constructor
A new instance of RangeConstraint.
- #validate(value, _context = {}) ⇒ Object
Methods inherited from Constraint
Constructor Details
#initialize(attribute_name, min:, max:) ⇒ RangeConstraint
Returns a new instance of RangeConstraint.
64 65 66 67 68 |
# File 'lib/ukiryu/validation/constraints.rb', line 64 def initialize(attribute_name, min:, max:) @attribute_name = attribute_name @min = min @max = max end |
Instance Attribute Details
#attribute_name ⇒ Object (readonly)
Returns the value of attribute attribute_name.
59 60 61 |
# File 'lib/ukiryu/validation/constraints.rb', line 59 def attribute_name @attribute_name end |
#max ⇒ Object (readonly)
Returns the value of attribute max.
59 60 61 |
# File 'lib/ukiryu/validation/constraints.rb', line 59 def max @max end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
59 60 61 |
# File 'lib/ukiryu/validation/constraints.rb', line 59 def min @min end |
Instance Method Details
#validate(value, _context = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ukiryu/validation/constraints.rb', line 71 def validate(value, _context = {}) return if value.nil? check_value = value.is_a?(Array) ? value.size : value return unless check_value < @min || check_value > @max raise ValidationIssue.new(@attribute_name, :range, "#{@attribute_name} must be between #{@min} and #{@max}, got #{check_value}") end |