From 306be3ffb8fc07c07058f8d04286bc664b4abe2e Mon Sep 17 00:00:00 2001 From: naotake51 Date: Sun, 17 Dec 2023 19:59:23 +0900 Subject: [PATCH] Ensure `@rule` is never applied to argument lists themselves --- src/Validation/RulesGatherer.php | 11 ++-- .../Validation/RulesDirectiveTest.php | 50 +++++++++++++++++++ 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/Validation/RulesGatherer.php b/src/Validation/RulesGatherer.php index d4c29121d5..3749dde10e 100644 --- a/src/Validation/RulesGatherer.php +++ b/src/Validation/RulesGatherer.php @@ -60,12 +60,11 @@ protected function gatherRulesRecursively(ArgumentSet $argumentSet, array $argum Utils::instanceofMatcher(ArgDirective::class), ); - if ( - $argument->type instanceof ListType - && is_array($argument->value) - ) { - foreach ($argument->value as $index => $value) { - $this->handleArgumentValue($value, $directivesForArgument, array_merge($nestedPath, [$index])); + if ($argument->type instanceof ListType) { + if (is_array($argument->value)) { + foreach ($argument->value as $index => $value) { + $this->handleArgumentValue($value, $directivesForArgument, array_merge($nestedPath, [$index])); + } } } else { $this->handleArgumentValue($argument->value, $directivesForArgument, $nestedPath); diff --git a/tests/Integration/Validation/RulesDirectiveTest.php b/tests/Integration/Validation/RulesDirectiveTest.php index 46b4aab58b..f3a6a928ac 100644 --- a/tests/Integration/Validation/RulesDirectiveTest.php +++ b/tests/Integration/Validation/RulesDirectiveTest.php @@ -306,4 +306,54 @@ public static function invalidMessageArguments(): array [/** @lang GraphQL */ '[{rule: 3, message: "asfd"}]'], ]; } + + public function testValidateElementsOfListType(): void + { + $this->schema = /** @lang GraphQL */ ' + type Query { + foo( + bar: [String] + @rules( + apply: ["required"] + ) + ): String + } + '; + + $this + ->graphQL(/** @lang GraphQL */ ' + { + foo( + bar: ["", null, "bar"] + ) + } + ') + ->assertGraphQLValidationKeys(['bar.0', 'bar.1']); + + $this + ->graphQL(/** @lang GraphQL */ ' + { + foo( + bar: [] + ) + } + ') + ->assertJson([ + 'data' => [ + 'foo' => Foo::THE_ANSWER, + ], + ]); + + $this + ->graphQL(/** @lang GraphQL */ ' + { + foo + } + ') + ->assertJson([ + 'data' => [ + 'foo' => Foo::THE_ANSWER, + ], + ]); + } }