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/allow_null_validator.rb,
lib/graphql/schema/validator/allow_blank_validator.rb,
lib/graphql/schema/validator/numericality_validator.rb
Direct Known Subclasses
AllowBlankValidator, AllowNullValidator, ExclusionValidator, FormatValidator, InclusionValidator, LengthValidator, NumericalityValidator, RequiredValidator
Defined Under Namespace
Classes: AllowBlankValidator, AllowNullValidator, 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
-
#initialize(validated:, allow_blank: false, allow_null: false) ⇒ Validator
constructor
A new instance of Validator.
-
#partial_format(string, substitutions) ⇒ Object
This is like
String#%
, but it supports the case that only some ofstring
's values are present insubstitutions
. -
#permitted_empty_value?(value) ⇒ Boolean
true
ifvalue
isnil
and this validator hasallow_null: true
or if value is.blank?
and this validator hasallow_blank: true
. -
#validate(object, context, value) ⇒ nil, ...
Error message or messages to add.
Constructor Details
#initialize(validated:, allow_blank: false, allow_null: false) ⇒ Validator
Returns a new instance of Validator.
13 14 15 16 17 |
# File 'lib/graphql/schema/validator.rb', line 13 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.
103 104 105 |
# File 'lib/graphql/schema/validator.rb', line 103 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>
46 47 48 49 50 51 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 79 80 81 82 |
# File 'lib/graphql/schema/validator.rb', line 46 def self.from_config(schema_member, validates_hash) if validates_hash.nil? || validates_hash.empty? EMPTY_ARRAY else validates_hash = validates_hash.dup allow_null = validates_hash.delete(:allow_null) allow_blank = validates_hash.delete(:allow_blank) # This could be {...}.compact on Ruby 2.4+ = {} if !allow_null.nil? [:allow_null] = allow_null end if !allow_blank.nil? [:allow_blank] = allow_blank end # allow_nil or allow_blank are the _only_ validations: if validates_hash.empty? validates_hash = end 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 if .is_a?(Hash) validator_class.new(validated: schema_member, **(.merge())) else validator_class.new(, validated: schema_member, **) end 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
).
89 90 91 92 |
# File 'lib/graphql/schema/validator.rb', line 89 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
97 98 99 100 |
# File 'lib/graphql/schema/validator.rb', line 97 def self.uninstall(name) all_validators.delete(name) nil end |
.validate!(validators, object, context, value, as: nil) ⇒ void
This method returns an undefined value.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/graphql/schema/validator.rb', line 125 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.validate(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
#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
29 30 31 32 33 34 35 |
# File 'lib/graphql/schema/validator.rb', line 29 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 |
#permitted_empty_value?(value) ⇒ Boolean
Returns true
if value
is nil
and this validator has allow_null: true
or if value is .blank?
and this validator has allow_blank: true
.
38 39 40 41 |
# File 'lib/graphql/schema/validator.rb', line 38 def permitted_empty_value?(value) (value.nil? && @allow_null) || (@allow_blank && value.respond_to?(:blank?) && value.blank?) end |
#validate(object, context, value) ⇒ nil, ...
Returns Error message or messages to add.
23 24 25 |
# File 'lib/graphql/schema/validator.rb', line 23 def validate(object, context, value) raise GraphQL::RequiredImplementationMissingError, "Validator classes should implement #validate" end |