Class: GraphQL::Schema::Validator::AllValidator
- Inherits:
-
Validator
- Object
- Validator
- GraphQL::Schema::Validator::AllValidator
- Defined in:
- lib/graphql/schema/validator/all_validator.rb
Overview
Use this to validate each member of an array value.
Examples
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 } } }
Instance Method Summary collapse
-
#initialize(validated:, allow_blank: false, allow_null: false, **validators) ⇒ AllValidator
constructor
validatorsis configuration for other validators, for example{ format: { with: /.../ } }. -
#validate(object, context, value) ⇒ Object
: (untyped object, GraphQL::Query::Context context, untyped value) -> Array.
Constructor Details
#initialize(validated:, allow_blank: false, allow_null: false, **validators) ⇒ AllValidator
validators is configuration for other validators, for example { format: { with: /.../ } }.
: (validated: GraphQL::Schema::Argument | GraphQL::Schema::Field | singleton(GraphQL::Schema::Resolver) | singleton(GraphQL::Schema::InputObject), ?allow_blank: bool, ?allow_null: bool, **untyped validators) -> void
35 36 37 38 39 |
# File 'lib/graphql/schema/validator/all_validator.rb', line 35 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) #: Array[GraphQL::Schema::Validator] end |
Instance Method Details
#validate(object, context, value) ⇒ Object
: (untyped object, GraphQL::Query::Context context, untyped value) -> Array
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/graphql/schema/validator/all_validator.rb', line 42 def validate(object, context, value) return EMPTY_ARRAY if permitted_empty_value?(value) all_errors = EMPTY_ARRAY #: Array[String] 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 |