Sha256: cfd8dda772c5b8d5bf30457ac83911b073859139d4468cd16b7e3e7381a878a1

Contents?: true

Size: 1.42 KB

Versions: 111

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
        # @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 permitted_empty_value?(value)
            # Do nothing
          elsif value.nil? ||
              (@with_pattern && !value.match?(@with_pattern)) ||
              (@without_pattern && value.match?(@without_pattern))
            @message
          end
        end
      end
    end
  end
end

Version data entries

111 entries across 111 versions & 2 rubygems

Version Path
graphql-2.4.4 lib/graphql/schema/validator/format_validator.rb
graphql-2.4.3 lib/graphql/schema/validator/format_validator.rb
graphql-2.4.2 lib/graphql/schema/validator/format_validator.rb
graphql-2.4.1 lib/graphql/schema/validator/format_validator.rb
graphql-2.4.0 lib/graphql/schema/validator/format_validator.rb
graphql-2.3.20 lib/graphql/schema/validator/format_validator.rb
graphql-2.3.19 lib/graphql/schema/validator/format_validator.rb
graphql-2.3.18 lib/graphql/schema/validator/format_validator.rb
graphql-2.3.17 lib/graphql/schema/validator/format_validator.rb
graphql-2.3.16 lib/graphql/schema/validator/format_validator.rb
graphql-2.3.15 lib/graphql/schema/validator/format_validator.rb
graphql-2.3.14 lib/graphql/schema/validator/format_validator.rb
graphql-2.3.13 lib/graphql/schema/validator/format_validator.rb
graphql-2.3.12 lib/graphql/schema/validator/format_validator.rb
graphql-2.3.11 lib/graphql/schema/validator/format_validator.rb
graphql-2.3.10 lib/graphql/schema/validator/format_validator.rb
graphql-2.2.16 lib/graphql/schema/validator/format_validator.rb
graphql-2.1.13 lib/graphql/schema/validator/format_validator.rb
graphql-2.3.9 lib/graphql/schema/validator/format_validator.rb
graphql-2.3.8 lib/graphql/schema/validator/format_validator.rb