Class: GraphQL::Schema::Validator::ExclusionValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/graphql/schema/validator/exclusion_validator.rb

Overview

Use this to specifically reject values from an argument.

Examples

Example: disallow certain values

argument :favorite_non_prime, Integer, required: true,
  validates: { exclusion: { in: [2, 3, 5, 7, ... ]} }

Instance Method Summary collapse

Constructor Details

#initialize(in:, message: "%{validated} is reserved", **default_options) ⇒ ExclusionValidator

Parameters

  • message (String)
  • in (Array) — The values to reject

:call-seq:

initialize(String message:, Array in:, **default_options)

: (in: Array | Proc, ?message: String | Proc, **untyped default_options) -> void



27
28
29
30
31
32
# File 'lib/graphql/schema/validator/exclusion_validator.rb', line 27

def initialize(in:, message: "%{validated} is reserved", **default_options)
  # `in` is a reserved word, so work around that
  @in_list = binding.local_variable_get(:in) #: Array[untyped] | Proc
  @message = message #: String | Proc
  super(**default_options)
end

Instance Method Details

#validate(_object, _context, value) ⇒ Object

: (untyped _object, GraphQL::Query::Context _context, untyped value) -> String?



35
36
37
38
39
40
41
# File 'lib/graphql/schema/validator/exclusion_validator.rb', line 35

def validate(_object, _context, value)
  if permitted_empty_value?(value)
    # pass
  elsif validation_parameter(@in_list).include?(value)
    validation_parameter(@message)
  end
end