Method: GraphQL::Schema::Validator.from_config

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

.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