Class: GraphQL::Schema::Validator::NumericalityValidator
- Inherits:
-
GraphQL::Schema::Validator
- Object
- GraphQL::Schema::Validator
- GraphQL::Schema::Validator::NumericalityValidator
- Defined in:
- lib/graphql/schema/validator/numericality_validator.rb
Overview
Use this to assert numerical comparisons hold true for inputs.
Constant Summary
Constants included from FindInheritedValue::EmptyObjects
FindInheritedValue::EmptyObjects::EMPTY_ARRAY, FindInheritedValue::EmptyObjects::EMPTY_HASH
Instance Attribute Summary
Attributes inherited from GraphQL::Schema::Validator
Instance Method Summary collapse
-
#initialize(greater_than: nil, greater_than_or_equal_to: nil, less_than: nil, less_than_or_equal_to: nil, equal_to: nil, other_than: nil, odd: nil, even: nil, message: "%{validated} must be %{comparison} %{target}", **default_options) ⇒ NumericalityValidator
constructor
A new instance of NumericalityValidator.
-
#validate(object, context, value) ⇒ Object
Methods inherited from GraphQL::Schema::Validator
#apply, from_config, install, #partial_format, uninstall, validate!
Constructor Details
#initialize(greater_than: nil, greater_than_or_equal_to: nil, less_than: nil, less_than_or_equal_to: nil, equal_to: nil, other_than: nil, odd: nil, even: nil, message: "%{validated} must be %{comparison} %{target}", **default_options) ⇒ NumericalityValidator
Returns a new instance of NumericalityValidator.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/graphql/schema/validator/numericality_validator.rb', line 28 def initialize( greater_than: nil, greater_than_or_equal_to: nil, less_than: nil, less_than_or_equal_to: nil, equal_to: nil, other_than: nil, odd: nil, even: nil, message: "%{validated} must be %{comparison} %{target}", ** ) @greater_than = greater_than @greater_than_or_equal_to = greater_than_or_equal_to @less_than = less_than @less_than_or_equal_to = less_than_or_equal_to @equal_to = equal_to @other_than = other_than @odd = odd @even = even @message = super(**) end |
Instance Method Details
#validate(object, context, value) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/graphql/schema/validator/numericality_validator.rb', line 49 def validate(object, context, value) if @greater_than && value <= @greater_than partial_format(@message, { comparison: "greater than", target: @greater_than }) elsif @greater_than_or_equal_to && value < @greater_than_or_equal_to partial_format(@message, { comparison: "greater than or equal to", target: @greater_than_or_equal_to }) elsif @less_than && value >= @less_than partial_format(@message, { comparison: "less than", target: @less_than }) elsif @less_than_or_equal_to && value > @less_than_or_equal_to partial_format(@message, { comparison: "less than or equal to", target: @less_than_or_equal_to }) elsif @equal_to && value != @equal_to partial_format(@message, { comparison: "equal to", target: @equal_to }) elsif @other_than && value == @other_than partial_format(@message, { comparison: "something other than", target: @other_than }) elsif @even && !value.even? (partial_format(@message, { comparison: "even", target: "" })).strip elsif @odd && !value.odd? (partial_format(@message, { comparison: "odd", target: "" })).strip end end |