Skip to content

Commit

Permalink
Merge pull request #5048 from brandon-at-wrk/patch-1
Browse files Browse the repository at this point in the history
fix: avoid iterating on null array if all validator permits null value
  • Loading branch information
rmosolgo authored Aug 6, 2024
2 parents 907125c + 5ce95a4 commit f068799
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/graphql/schema/validator/all_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def initialize(validated:, allow_blank: false, allow_null: false, **validators)
end

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

all_errors = EMPTY_ARRAY

value.each do |subvalue|
Expand Down
16 changes: 16 additions & 0 deletions spec/graphql/schema/validator/all_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,20 @@
]

build_tests(:all, [Integer], expectations)

expectations = [
{
config: { allow_null: true, inclusion: { in: 1..5 }, numericality: { odd: true } },
cases: [
{ query: "{ validated(value: null) }", result: nil, error_messages: [] },
{ query: "{ validated(value: []) }", result: [], error_messages: [] },
{ query: "{ validated(value: [1]) }", result: [1], error_messages: [] },
{ query: "{ validated(value: [1, 3]) }", result: [1, 3], error_messages: [] },
{ query: "{ validated(value: [4]) }", result: nil, error_messages: ["value must be odd"] },
{ query: "{ validated(value: [7]) }", result: nil, error_messages: ["value is not included in the list"] },
],
},
]

build_tests(:all, [Integer], expectations)
end

0 comments on commit f068799

Please sign in to comment.