Class: GraphQL::Schema::Validator::RequiredValidator
- Inherits:
-
GraphQL::Schema::Validator
- Object
- GraphQL::Schema::Validator
- GraphQL::Schema::Validator::RequiredValidator
- Defined in:
- lib/graphql/schema/validator/required_validator.rb
Overview
Use this validator to require one of the named arguments to be present. Or, use Arrays of symbols to name a valid set of arguments.
(This is for specifying mutually exclusive sets of arguments.)
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(one_of: nil, argument: nil, message: "%{validated} has the wrong arguments", **default_options) ⇒ RequiredValidator
constructor
A new instance of RequiredValidator.
-
#validate(_object, _context, value) ⇒ Object
Methods inherited from GraphQL::Schema::Validator
from_config, install, #partial_format, #permitted_empty_value?, uninstall, validate!
Constructor Details
#initialize(one_of: nil, argument: nil, message: "%{validated} has the wrong arguments", **default_options) ⇒ RequiredValidator
Returns a new instance of RequiredValidator.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/graphql/schema/validator/required_validator.rb', line 40 def initialize(one_of: nil, argument: nil, message: "%{validated} has the wrong arguments", **) @one_of = if one_of one_of elsif argument [argument] else raise ArgumentError, "`one_of:` or `argument:` must be given in `validates required: {...}`" end @message = super(**) end |
Instance Method Details
#validate(_object, _context, value) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/graphql/schema/validator/required_validator.rb', line 52 def validate(_object, _context, value) matched_conditions = 0 if !value.nil? @one_of.each do |one_of_condition| case one_of_condition when Symbol if value.key?(one_of_condition) matched_conditions += 1 end when Array if one_of_condition.all? { |k| value.key?(k) } matched_conditions += 1 break end else raise ArgumentError, "Unknown one_of condition: #{one_of_condition.inspect}" end end end if matched_conditions == 1 nil # OK else @message end end |