Skip to content

Commit

Permalink
Ensure @rule is never applied to argument lists themselves
Browse files Browse the repository at this point in the history
  • Loading branch information
naotake51 authored Dec 17, 2023
1 parent a3962dc commit 306be3f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/Validation/RulesGatherer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
50 changes: 50 additions & 0 deletions tests/Integration/Validation/RulesDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
]);
}
}

0 comments on commit 306be3f

Please sign in to comment.