Sha256: c24ad3033ed32add64cebac2965cadcf5cd70c9d7c21b3aba32714b608651b5b

Contents?: true

Size: 1.01 KB

Versions: 19

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module GraphQL
  class Schema
    class Validator
      # You can use this to allow certain values for an argument.
      #
      # Usually, a {GraphQL::Schema::Enum} is better for this, because it's self-documenting.
      #
      # @example only allow certain values for an argument
      #
      #   argument :favorite_prime, Integer, required: true,
      #     validates: { inclusion: { in: [2, 3, 5, 7, 11, ... ] } }
      #
      class InclusionValidator < Validator
        # @param message [String]
        # @param in [Array] The values to allow
        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

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

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
graphql-1.12.18 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.17 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.16 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.15 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.14 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.13 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.12 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.11 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.10 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.9 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.8 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.7 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.6 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.5 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.4 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.3 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.2 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.1 lib/graphql/schema/validator/inclusion_validator.rb
graphql-1.12.0 lib/graphql/schema/validator/inclusion_validator.rb