Class: GraphQL::Schema::Validator::InclusionValidator
- Inherits:
-
Validator
- Object
- Validator
- GraphQL::Schema::Validator::InclusionValidator
- Defined in:
- lib/graphql/schema/validator/inclusion_validator.rb
Overview
You can use this to allow certain values for an argument.
Usually, a GraphQL::Schema::Enum is better for this, because it's self-documenting.
Examples
Example: only allow certain values for an argument
argument :favorite_prime, Integer, required: true,
validates: { inclusion: { in: [2, 3, 5, 7, 11, ... ] } }
Instance Method Summary collapse
-
#initialize(in:, message: "%{validated} is not included in the list", **default_options) ⇒ InclusionValidator
constructor
Parameters.
-
#validate(_object, _context, value) ⇒ Object
: (untyped _object, GraphQL::Query::Context _context, untyped value) -> String?.
Constructor Details
#initialize(in:, message: "%{validated} is not included in the list", **default_options) ⇒ InclusionValidator
Parameters
message(String)in(Array) — The values to allow
:call-seq:
initialize(Array in:, String message:, **default_options)
: (in: Array | Proc, ?message: String | Proc, **untyped default_options) -> void
29 30 31 32 33 34 |
# File 'lib/graphql/schema/validator/inclusion_validator.rb', line 29 def initialize(in:, message: "%{validated} is not included in the list", **) # `in` is a reserved word, so work around that @in_list = binding.local_variable_get(:in) #: Array[untyped] | Proc @message = #: String | Proc super(**) end |
Instance Method Details
#validate(_object, _context, value) ⇒ Object
: (untyped _object, GraphQL::Query::Context _context, untyped value) -> String?
37 38 39 40 41 42 43 |
# File 'lib/graphql/schema/validator/inclusion_validator.rb', line 37 def validate(_object, _context, value) if permitted_empty_value?(value) # pass elsif !validation_parameter(@in_list).include?(value) validation_parameter(@message) end end |