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

Inherits:
GraphQL::Schema::Validator show all
Defined in:
lib/graphql/schema/validator/exclusion_validator.rb

Overview

Use this to specifically reject values from an argument.

Examples:

disallow certain values


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

Constant Summary

Constants included from EmptyObjects

EmptyObjects::EMPTY_ARRAY, EmptyObjects::EMPTY_HASH

Instance Attribute Summary

Attributes inherited from GraphQL::Schema::Validator

#validated

Instance Method Summary collapse

Methods inherited from GraphQL::Schema::Validator

from_config, install, #partial_format, #permitted_empty_value?, uninstall, validate!

Constructor Details

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

Returns a new instance of ExclusionValidator.

Parameters:

  • message (String) (defaults to: "%{validated} is reserved")
  • in (Array)

    The values to reject



16
17
18
19
20
21
# File 'lib/graphql/schema/validator/exclusion_validator.rb', line 16

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

Instance Method Details

#validate(_object, _context, value) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/graphql/schema/validator/exclusion_validator.rb', line 23

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