Sha256: ba128d78c874d4cdda050f69efdcf82f955070652d7cd92a40dcbec6e57b8d3e

Contents?: true

Size: 943 Bytes

Versions: 11

Compression:

Stored size: 943 Bytes

Contents

# Test whether `ast_value` is a valid input for `type`
class GraphQL::StaticValidation::LiteralValidator
  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
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
graphql-0.9.5 lib/graphql/static_validation/literal_validator.rb
graphql-0.9.4 lib/graphql/static_validation/literal_validator.rb
graphql-0.9.3 lib/graphql/static_validation/literal_validator.rb
graphql-0.9.2 lib/graphql/static_validation/literal_validator.rb
graphql-0.8.1 lib/graphql/static_validation/literal_validator.rb
graphql-0.8.0 lib/graphql/static_validation/literal_validator.rb
graphql-0.7.1 lib/graphql/static_validation/literal_validator.rb
graphql-0.7.0 lib/graphql/static_validation/literal_validator.rb
graphql-0.6.2 lib/graphql/static_validation/literal_validator.rb
graphql-0.6.1 lib/graphql/static_validation/literal_validator.rb
graphql-0.6.0 lib/graphql/static_validation/literal_validator.rb