Sha256: 57e4f2b2a97a0c6227a55f3c69d98dec4752ab9a559d339a917c637572ce92ed

Contents?: true

Size: 1.83 KB

Versions: 25

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module GraphQL
  class Schema
    class Validator
      # Use this to validate each member of an array value.
      #
      # @example validate format of all strings in an array
      #
      #   argument :handles, [String],
      #     validates: { all: { format: { with: /\A[a-z0-9_]+\Z/ } } }
      #
      # @example multiple validators can be combined
      #
      #   argument :handles, [String],
      #     validates: { all: { format: { with: /\A[a-z0-9_]+\Z/ }, length: { maximum: 32 } } }
      #
      # @example any type can be used
      #
      #   argument :choices, [Integer],
      #     validates: { all: { inclusion: { in: 1..12 } } }
      #
      class AllValidator < Validator
        def initialize(validated:, allow_blank: false, allow_null: false, **validators)
          super(validated: validated, allow_blank: allow_blank, allow_null: allow_null)

          @validators = Validator.from_config(validated, validators)
        end

        def validate(object, context, value)
          return EMPTY_ARRAY if permitted_empty_value?(value)

          all_errors = EMPTY_ARRAY

          value.each do |subvalue|
            @validators.each do |validator|
              errors = validator.validate(object, context, subvalue)
              if errors &&
                  (errors.is_a?(Array) && errors != EMPTY_ARRAY) ||
                  (errors.is_a?(String))
                if all_errors.frozen? # It's empty
                  all_errors = []
                end
                if errors.is_a?(String)
                  all_errors << errors
                else
                  all_errors.concat(errors)
                end
              end
            end
          end

          unless all_errors.frozen?
            all_errors.uniq!
          end

          all_errors
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
graphql-2.4.14 lib/graphql/schema/validator/all_validator.rb
graphql-2.3.22 lib/graphql/schema/validator/all_validator.rb
graphql-2.3.21 lib/graphql/schema/validator/all_validator.rb
graphql-2.4.13 lib/graphql/schema/validator/all_validator.rb
graphql-2.4.12 lib/graphql/schema/validator/all_validator.rb
graphql-2.4.11 lib/graphql/schema/validator/all_validator.rb
graphql-2.4.10 lib/graphql/schema/validator/all_validator.rb
graphql-2.4.9 lib/graphql/schema/validator/all_validator.rb
graphql-2.4.8 lib/graphql/schema/validator/all_validator.rb
graphql-2.4.7 lib/graphql/schema/validator/all_validator.rb
graphql-2.4.6 lib/graphql/schema/validator/all_validator.rb
graphql-2.4.5 lib/graphql/schema/validator/all_validator.rb
graphql-2.4.4 lib/graphql/schema/validator/all_validator.rb
graphql-2.4.3 lib/graphql/schema/validator/all_validator.rb
graphql-2.4.2 lib/graphql/schema/validator/all_validator.rb
graphql-2.4.1 lib/graphql/schema/validator/all_validator.rb
graphql-2.4.0 lib/graphql/schema/validator/all_validator.rb
graphql-2.3.20 lib/graphql/schema/validator/all_validator.rb
graphql-2.3.19 lib/graphql/schema/validator/all_validator.rb
graphql-2.3.18 lib/graphql/schema/validator/all_validator.rb