Class: GraphQL::Schema::Validator
- Inherits:
-
Object
- Object
- GraphQL::Schema::Validator
- Includes:
- FindInheritedValue::EmptyObjects
- Defined in:
- lib/graphql/schema/validator.rb,
lib/graphql/schema/validator/format_validator.rb,
lib/graphql/schema/validator/length_validator.rb,
lib/graphql/schema/validator/required_validator.rb,
lib/graphql/schema/validator/exclusion_validator.rb,
lib/graphql/schema/validator/inclusion_validator.rb,
lib/graphql/schema/validator/numericality_validator.rb
Direct Known Subclasses
ExclusionValidator, FormatValidator, InclusionValidator, LengthValidator, NumericalityValidator, RequiredValidator
Defined Under Namespace
Classes: ExclusionValidator, FormatValidator, InclusionValidator, LengthValidator, NumericalityValidator, RequiredValidator, ValidationFailedError
Constant Summary
Constants included from FindInheritedValue::EmptyObjects
FindInheritedValue::EmptyObjects::EMPTY_ARRAY, FindInheritedValue::EmptyObjects::EMPTY_HASH
Class Attribute Summary collapse
-
.all_validators ⇒ Object
Returns the value of attribute all_validators.
Instance Attribute Summary collapse
-
#validated ⇒ GraphQL::Schema::Argument, ...
readonly
The thing being validated.
Class Method Summary collapse
-
.from_config(schema_member, validates_hash) ⇒ Array<Validator>
-
.install(name, validator_class) ⇒ void
Add
validator_class
to be initialized whenvalidates:
is givenname
. -
.uninstall(name) ⇒ void
Remove whatever validator class is Validator.installed at
name
, if there is one. -
.validate!(validators, object, context, value, as: nil) ⇒ void
Instance Method Summary collapse
-
#apply(object, context, value) ⇒ Object
private
This is called by the validation system and eventually calls #validate.
-
#initialize(validated:, allow_blank: false, allow_null: false) ⇒ Validator
constructor
TODO should this implement
if:
andunless:
?. -
#partial_format(string, substitutions) ⇒ Object
This is like
String#%
, but it supports the case that only some ofstring
’s values are present insubstitutions
. -
#validate(object, context, value) ⇒ nil, ...
Error message or messages to add.
Constructor Details
#initialize(validated:, allow_blank: false, allow_null: false) ⇒ Validator
TODO should this implement if:
and unless:
?
14 15 16 17 18 |
# File 'lib/graphql/schema/validator.rb', line 14 def initialize(validated:, allow_blank: false, allow_null: false) @validated = validated @allow_blank = allow_blank @allow_null = allow_null end |
Class Attribute Details
.all_validators ⇒ Object
Returns the value of attribute all_validators.
96 97 98 |
# File 'lib/graphql/schema/validator.rb', line 96 def all_validators @all_validators end |
Instance Attribute Details
#validated ⇒ GraphQL::Schema::Argument, ... (readonly)
The thing being validated
8 9 10 |
# File 'lib/graphql/schema/validator.rb', line 8 def validated @validated end |
Class Method Details
.from_config(schema_member, validates_hash) ⇒ Array<Validator>
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/graphql/schema/validator.rb', line 61 def self.from_config(schema_member, validates_hash) if validates_hash.nil? || validates_hash.empty? EMPTY_ARRAY else validates_hash.map do |validator_name, | validator_class = case validator_name when Class validator_name else all_validators[validator_name] || raise(ArgumentError, "unknown validation: #{validator_name.inspect}") end validator_class.new(validated: schema_member, **) end end end |
.install(name, validator_class) ⇒ void
This method returns an undefined value.
Add validator_class
to be initialized when validates:
is given name
.
(It’s initialized with whatever options are given by the key name
).
82 83 84 85 |
# File 'lib/graphql/schema/validator.rb', line 82 def self.install(name, validator_class) all_validators[name] = validator_class nil end |
.uninstall(name) ⇒ void
This method returns an undefined value.
Remove whatever validator class is installed at name
, if there is one
90 91 92 93 |
# File 'lib/graphql/schema/validator.rb', line 90 def self.uninstall(name) all_validators.delete(name) nil end |
.validate!(validators, object, context, value, as: nil) ⇒ void
This method returns an undefined value.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/graphql/schema/validator.rb', line 118 def self.validate!(validators, object, context, value, as: nil) # Assuming the default case is no errors, reduce allocations in that case. # This will be replaced with a mutable array if we actually get any errors. all_errors = EMPTY_ARRAY validators.each do |validator| validated = as || validator.validated errors = validator.apply(object, context, value) if errors && (errors.is_a?(Array) && errors != EMPTY_ARRAY) || (errors.is_a?(String)) if all_errors.frozen? # It's empty all_errors = [] end interpolation_vars = { validated: validated.graphql_name } if errors.is_a?(String) all_errors << (errors % interpolation_vars) else errors = errors.map { |e| e % interpolation_vars } all_errors.concat(errors) end end end if all_errors.any? raise ValidationFailedError.new(errors: all_errors) end nil end |
Instance Method Details
#apply(object, context, value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This is called by the validation system and eventually calls #validate.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/graphql/schema/validator.rb', line 30 def apply(object, context, value) if value.nil? if @allow_null nil # skip this else "%{validated} can't be null" end elsif value.respond_to?(:blank?) && value.blank? if @allow_blank nil # skip this else "%{validated} can't be blank" end else validate(object, context, value) end end |
#partial_format(string, substitutions) ⇒ Object
This is like String#%
, but it supports the case that only some of string
’s
values are present in substitutions
50 51 52 53 54 55 56 |
# File 'lib/graphql/schema/validator.rb', line 50 def partial_format(string, substitutions) substitutions.each do |key, value| sub_v = value.is_a?(String) ? value : value.to_s string = string.gsub("%{#{key}}", sub_v) end string end |
#validate(object, context, value) ⇒ nil, ...
Returns Error message or messages to add.
24 25 26 |
# File 'lib/graphql/schema/validator.rb', line 24 def validate(object, context, value) raise GraphQL::RequiredImplementationMissingError, "Validator classes should implement #validate" end |