Method: GraphQL::Schema::Validator.validate!

Defined in:
lib/graphql/schema/validator.rb

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



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