Class: GraphQL::Schema::Validator::InclusionValidator

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

Overview

You can use this to allow certain values for an argument.

Usually, a Enum is better for this, because it's self-documenting.

Examples:

only allow certain values for an argument


argument :favorite_prime, Integer, required: true,
  validates: { inclusion: { in: [2, 3, 5, 7, 11, ... ] } }

Constant Summary

Constants included from FindInheritedValue::EmptyObjects

FindInheritedValue::EmptyObjects::EMPTY_ARRAY, FindInheritedValue::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(in:, message: "%{validated} is not included in the list", **default_options) ⇒ InclusionValidator

Returns a new instance of InclusionValidator.

Parameters:

  • message (String) (defaults to: "%{validated} is not included in the list")
  • in (Array)

    The values to allow



18
19
20
21
22
23
# File 'lib/graphql/schema/validator/inclusion_validator.rb', line 18

def initialize(in:, message: "%{validated} is not included in the list", **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



25
26
27
28
29
30
31
# File 'lib/graphql/schema/validator/inclusion_validator.rb', line 25

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