Class: GraphQL::Schema::Validator

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(validated:, allow_blank: false, allow_null: false) ⇒ Validator

Returns a new instance of Validator.

Parameters:



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_validatorsObject

Returns the value of attribute all_validators.



100
101
102
# File 'lib/graphql/schema/validator.rb', line 100

def all_validators
  @all_validators
end

Instance Attribute Details

#validatedGraphQL::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>

Parameters:

Returns:



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
# 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

    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

.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).

Parameters:

  • name (Symbol)
  • validator_class (Class)


86
87
88
89
# File 'lib/graphql/schema/validator.rb', line 86

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

Parameters:

  • name (Symbol)


94
95
96
97
# File 'lib/graphql/schema/validator.rb', line 94

def self.uninstall(name)
  all_validators.delete(name)
  nil
end

.validate!(validators, object, context, value, as: nil) ⇒ void

This method returns an undefined value.

Parameters:



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
147
148
149
150
# File 'lib/graphql/schema/validator.rb', line 122

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



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.

Returns:

  • (Boolean)

    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.

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)

    Error message or messages to add

Raises:



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