Class: GraphQL::Schema::Validator
- Inherits:
-
Object
- Object
- GraphQL::Schema::Validator
- Includes:
- EmptyObjects
- Defined in:
- lib/graphql/schema/validator.rb,
lib/graphql/schema/validator/all_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
Defined Under Namespace
Classes: AllValidator, AllowBlankValidator, AllowNullValidator, ExclusionValidator, FormatValidator, InclusionValidator, LengthValidator, NumericalityValidator, RequiredValidator, ValidationFailedError
Constant Summary
Constants included from EmptyObjects
EmptyObjects::EMPTY_ARRAY, EmptyObjects::EMPTY_HASH
Class Attribute Summary collapse
-
.all_validators ⇒ Object
Returns the value of attribute all_validators.
Instance Attribute Summary collapse
-
#validated ⇒ Object
readonly
The thing being validated.
Class Method Summary collapse
-
.from_config(schema_member, validates_hash) ⇒ Object
Parameters.
-
.install(name, validator_class) ⇒ Object
Add
validator_classto be initialized whenvalidates:is givenname. -
.uninstall(name) ⇒ Object
Remove whatever validator class is .installed at
name, if there is one. -
.validate!(validators, object, context, value, as: nil) ⇒ Object
Parameters.
Instance Method Summary collapse
-
#initialize(validated:, allow_blank: false, allow_null: false) ⇒ Validator
constructor
Parameters.
-
#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
Returns.
-
#validate(object, context, value) ⇒ Object
Parameters.
-
#validation_parameter(config_value) ⇒ Object
Returns.
Constructor Details
#initialize(validated:, allow_blank: false, allow_null: false) ⇒ Validator
Parameters
validated(GraphQL::Schema::Argument, GraphQL::Schema::Field, GraphQL::Schema::Resolver, Class<GraphQL::Schema::InputObject>) — The argument or argument owner this validator is attached toallow_blank(Boolean) — iftrue, then objects that respond to.blank?and return true for.blank?will skip this validationallow_null(Boolean) — iftrue, then incomingnulls will skip this validation
:call-seq:
initialize(GraphQL::Schema::Argument | GraphQL::Schema::Field | GraphQL::Schema::Resolver | Class[GraphQL::Schema::InputObject] validated:, bool allow_blank:, bool allow_null:)
23 24 25 26 27 |
# File 'lib/graphql/schema/validator.rb', line 23 def initialize(validated:, allow_blank: false, allow_null: false) @validated = validated @allow_blank = allow_blank #: bool | Proc | nil @allow_null = allow_null #: bool | Proc | nil end |
Class Attribute Details
.all_validators ⇒ Object
Returns the value of attribute all_validators.
163 164 165 |
# File 'lib/graphql/schema/validator.rb', line 163 def all_validators @all_validators end |
Instance Attribute Details
#validated ⇒ Object (readonly)
The thing being validated
Returns
GraphQL::Schema::Argument, GraphQL::Schema::Field, GraphQL::Schema::Resolver, Class<GraphQL::Schema::InputObject>
13 14 15 |
# File 'lib/graphql/schema/validator.rb', line 13 def validated @validated end |
Class Method Details
.from_config(schema_member, validates_hash) ⇒ Object
Parameters
schema_member(GraphQL::Schema::Field, GraphQL::Schema::Argument, Class<GraphQL::Schema::InputObject>)validates_hash(Hash{Symbol => Hash}, Hash{Class => Hash} nil) — A configuration passed asvalidates:
Returns
Array<Validator>
:call-seq:
from_config(GraphQL::Schema::Field | GraphQL::Schema::Argument | Class[GraphQL::Schema::InputObject] schema_member, Hash[Symbol, Hash] | Hash[Class, Hash] nil validates_hash) -> Array[Validator]
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/graphql/schema/validator.rb', line 91 def self.from_config(schema_member, validates_hash) if validates_hash.nil? || validates_hash.empty? EMPTY_ARRAY else validates_hash = validates_hash.dup = {} if validates_hash[:allow_null] [:allow_null] = validates_hash.delete(:allow_null) end if validates_hash[:allow_blank] [:allow_blank] = validates_hash.delete(: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) ⇒ Object
Add validator_class to be initialized when validates: is given name.
(It's initialized with whatever options are given by the key name).
Parameters
name(Symbol)validator_class(Class)
Returns
void
:call-seq:
install(Symbol name, Class validator_class) -> void
140 141 142 143 |
# File 'lib/graphql/schema/validator.rb', line 140 def self.install(name, validator_class) all_validators[name] = validator_class nil end |
.uninstall(name) ⇒ Object
Remove whatever validator class is .installed at name, if there is one
Parameters
name(Symbol)
Returns
void
:call-seq:
uninstall(Symbol name) -> void
157 158 159 160 |
# File 'lib/graphql/schema/validator.rb', line 157 def self.uninstall(name) all_validators.delete(name) nil end |
.validate!(validators, object, context, value, as: nil) ⇒ Object
Parameters
validators(Array<Validator>)object(Object)context(Query::Context)value(Object)
Returns
void
Raises
ValidationFailedError
:call-seq:
validate!(Array[Validator] validators, Object object, Query::Context context, Object value, as:) -> void | ValidationFailedError
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/graphql/schema/validator.rb', line 196 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, value: value.inspect } 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.empty? 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
47 48 49 50 51 52 53 |
# File 'lib/graphql/schema/validator.rb', line 47 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
Boolean—trueifvalueisniland this validator hasallow_null: trueor if value is.blank?and this validator hasallow_blank: true
:call-seq:
permitted_empty_value?(value) -> bool
75 76 77 78 |
# File 'lib/graphql/schema/validator.rb', line 75 def permitted_empty_value?(value) (value.nil? && @allow_null) || (@allow_blank && value.respond_to?(:blank?) && value.blank?) end |
#validate(object, context, value) ⇒ Object
Parameters
object(Object) — The application object that this argument's field is being resolved forcontext(GraphQL::Query::Context)value(Object) — The client-provided value for this argument (after parsing and coercing by the input type)
Returns
nil, Array<String>, String— Error message or messages to add
:call-seq:
validate(Object object, GraphQL::Query::Context context, Object value) -> nil | Array[String] | String
41 42 43 |
# File 'lib/graphql/schema/validator.rb', line 41 def validate(object, context, value) raise GraphQL::RequiredImplementationMissingError, "Validator classes should implement #validate" end |
#validation_parameter(config_value) ⇒ Object
Returns
Object— The current value to use for validation, based onconfig_valuefrom configuration time. If a Proc is given, this calls it and returns it.
:call-seq:
validation_parameter(config_value) -> Object
61 62 63 64 65 66 67 |
# File 'lib/graphql/schema/validator.rb', line 61 def validation_parameter(config_value) if config_value.is_a?(Proc) config_value.call else config_value end end |