Sha256: 8992850fad9c6e1e4d96d2457e19d65633ec119e585c4e141213f7caf4bbc017

Contents?: true

Size: 1.42 KB

Versions: 19

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module GraphQL
  class Schema
    class Validator
      # Use this to assert that string values match (or don't match) the given RegExp.
      #
      # @example requiring input to match a pattern
      #
      #   argument :handle, String, required: true,
      #     validates: { format: { with: /\A[a-z0-9_]+\Z/ } }
      #
      # @example reject inputs that match a pattern
      #
      #   argument :word_that_doesnt_begin_with_a_vowel, String, required: true,
      #     validates: { format: { without: /\A[aeiou]/ } }
      #
      #   # It's pretty hard to come up with a legitimate use case for `without:`
      #
      class FormatValidator < Validator
        if !String.method_defined?(:match?)
          using GraphQL::StringMatchBackport
        end

        # @param with [RegExp, nil]
        # @param without [Regexp, nil]
        # @param message [String]
        def initialize(
          with: nil,
          without: nil,
          message: "%{validated} is invalid",
          **default_options
        )
          @with_pattern = with
          @without_pattern = without
          @message = message
          super(**default_options)
        end

        def validate(_object, _context, value)
          if (@with_pattern && !value.match?(@with_pattern)) ||
              (@without_pattern && value.match?(@without_pattern))
            @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/format_validator.rb
graphql-1.12.17 lib/graphql/schema/validator/format_validator.rb
graphql-1.12.16 lib/graphql/schema/validator/format_validator.rb
graphql-1.12.15 lib/graphql/schema/validator/format_validator.rb
graphql-1.12.14 lib/graphql/schema/validator/format_validator.rb
graphql-1.12.13 lib/graphql/schema/validator/format_validator.rb
graphql-1.12.12 lib/graphql/schema/validator/format_validator.rb
graphql-1.12.11 lib/graphql/schema/validator/format_validator.rb
graphql-1.12.10 lib/graphql/schema/validator/format_validator.rb
graphql-1.12.9 lib/graphql/schema/validator/format_validator.rb
graphql-1.12.8 lib/graphql/schema/validator/format_validator.rb
graphql-1.12.7 lib/graphql/schema/validator/format_validator.rb
graphql-1.12.6 lib/graphql/schema/validator/format_validator.rb
graphql-1.12.5 lib/graphql/schema/validator/format_validator.rb
graphql-1.12.4 lib/graphql/schema/validator/format_validator.rb
graphql-1.12.3 lib/graphql/schema/validator/format_validator.rb
graphql-1.12.2 lib/graphql/schema/validator/format_validator.rb
graphql-1.12.1 lib/graphql/schema/validator/format_validator.rb
graphql-1.12.0 lib/graphql/schema/validator/format_validator.rb