class GraphQL::Schema::Validator
Attributes
The thing being validated
Returns
-
GraphQL::Schema::Argument, GraphQL::Schema::Field, GraphQL::Schema::Resolver, Class<GraphQL::Schema::InputObject>
Public Class Methods
# File lib/graphql/schema/validator.rb, line 90 def self.from_config(schema_member, validates_hash) if validates_hash.nil? || validates_hash.empty? EMPTY_ARRAY else validates_hash = validates_hash.dup default_options = {} if validates_hash[:allow_null] default_options[:allow_null] = validates_hash.delete(:allow_null) end if validates_hash[:allow_blank] default_options[:allow_blank] = validates_hash.delete(:allow_blank) end # allow_nil or allow_blank are the _only_ validations: if validates_hash.empty? validates_hash = default_options end validates_hash.map do |validator_name, options| validator_class = case validator_name when Class validator_name else all_validators[validator_name] || raise(ArgumentError, "unknown validation: #{validator_name.inspect}") end if options.is_a?(Hash) validator_class.new(validated: schema_member, **(default_options.merge(options))) else validator_class.new(options, validated: schema_member, **default_options) end end end end
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>
# File lib/graphql/schema/validator.rb, line 139 def self.install(name, validator_class) all_validators[name] = validator_class nil end
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
# File lib/graphql/schema/validator.rb, line 22 def initialize(validated:, allow_blank: false, allow_null: false) @validated = validated @allow_blank = allow_blank @allow_null = allow_null end
Parameters
-
validated(GraphQL::Schema::Argument, GraphQL::Schema::Field, GraphQL::Schema::Resolver, Class<GraphQL::Schema::InputObject>) β The argument or argument owner this validator is attached to -
allow_blank(Boolean) β iftrue, then objects that respond to.blank?and return true for.blank?will skip this validation -
allow_null(Boolean) β iftrue, then incomingnulls will skip this validation
# File lib/graphql/schema/validator.rb, line 156 def self.uninstall(name) all_validators.delete(name) nil end
Remove whatever validator class is .installed at name, if there is one
Parameters
-
name(Symbol)
Returns
-
void
# File lib/graphql/schema/validator.rb, line 195 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
Parameters
-
validators(Array<Validator>) -
object(Object) -
context(Query::Context) -
value(Object)
Returns
-
void
Raises
Public Instance Methods
# File lib/graphql/schema/validator.rb, line 46 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
This is like String#%, but it supports the case that only some of stringβs values are present in substitutions
# File lib/graphql/schema/validator.rb, line 74 def permitted_empty_value?(value) (value.nil? && @allow_null) || (@allow_blank && value.respond_to?(:blank?) && value.blank?) end
Returns
-
Booleanβtrueifvalueisniland this validator hasallow_null: trueor if value is.blank?and this validator hasallow_blank: true
# File lib/graphql/schema/validator.rb, line 40 def validate(object, context, value) raise GraphQL::RequiredImplementationMissingError, "Validator classes should implement #validate" end
Parameters
-
object(Object) β The application object that this argumentβs field is being resolved for -
context(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βErrormessage or messages to add
# File lib/graphql/schema/validator.rb, line 60 def validation_parameter(config_value) if config_value.is_a?(Proc) config_value.call else config_value end end
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.