class GraphQL::Schema::Validation

This module provides a function for validating GraphQL types.

Its {RULES} contain objects that respond to `#call(type)`. Rules are looked up for given types (by class ancestry), then applied to the object until an error is returned.

Constants

RULES

A mapping of `{Class => [Proc, Proc…]}` pairs. To validate an instance, find entries where `object.is_a?(key)` is true. Then apply each rule from the matching values.

Public Class Methods

validate(object) click to toggle source

Lookup the rules for `object` based on its class, Then returns an error message or `nil` @param object [Object] something to be validated @return [String, Nil] error message, if there was one

# File lib/graphql/schema/validation.rb, line 14
def self.validate(object)
  rules = RULES.reduce([]) do |memo, (parent_class, validations)|
    memo + (object.is_a?(parent_class) ? validations : [])
  end
  # Stops after the first error
  rules.reduce(nil) { |memo, rule| memo || rule.call(object) }
end