Class: GraphQL::StaticValidation::LiteralValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/static_validation/literal_validator.rb

Overview

Test whether ‘ast_value` is a valid input for `type`

Instance Method Summary collapse

Instance Method Details

#validate(ast_value, type) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/graphql/static_validation/literal_validator.rb', line 3

def validate(ast_value, type)
  if type.kind.non_null?
    (!ast_value.nil?) && validate(ast_value, type.of_type)
  elsif type.kind.list? && ast_value.is_a?(Array)
    item_type = type.of_type
    ast_value.all? { |val| validate(val, item_type) }
  elsif type.kind.scalar?
    !type.coerce(ast_value).nil?
  elsif type.kind.enum? && ast_value.is_a?(GraphQL::Language::Nodes::Enum)
    !type.coerce(ast_value.name).nil?
  elsif type.kind.input_object? && ast_value.is_a?(GraphQL::Language::Nodes::InputObject)
    fields = type.input_fields
    ast_value.pairs.all? do |value|
      field_type = fields[value.name].type
      present_if_required = field_type.kind.non_null? ? !value.nil? : true
      present_if_required && validate(value.value, field_type)
    end
  else
    false
  end
end