Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If the ListType field is not present, the rule directive is applied to the array itself. #2488

Closed
naotake51 opened this issue Dec 15, 2023 · 3 comments
Labels
bug An error within Lighthouse

Comments

@naotake51
Copy link
Contributor

Describe the bug

If the ListType field is not present, the rule directive is applied to the array itself.

I expect the rules directive for ListType Field to always apply to array elements.

Expected behavior/Solution

the solution I think

https://github.com/nuwave/lighthouse/blob/v6.28.0/src/Validation/RulesGatherer.php#L63-L72

rewrite to

            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);
            }

Steps to reproduce

  1. Define a ListType field with a rules directive.
deleteHoges(
    ids: [ID!] @rules(apply: ["required", "uuid"])
): [Hoge!]!
  1. Run the query without filling out the ListType field.
mutation {
    deleteHoges {
        id
    }
}
  1. The following validation rule is created and an error occurs
["ids" => ["Required", "Uuid"]]

expected ListType rules derective apply to array elements

["ids.0" => ["Required", "Uuid"]]

Lighthouse Version

v6.22.0

@spawnia spawnia added the needs reproduction Failing test case needed label Dec 15, 2023
@spawnia
Copy link
Collaborator

spawnia commented Dec 15, 2023

The proposed solution looks fine. Can you add a pull request? Please start with a failing test case, probably in RulesDirectiveTest.

@naotake51
Copy link
Contributor Author

@spawnia

#2489

I created it here.

Please let me know if you have any problems.

@spawnia spawnia added bug An error within Lighthouse and removed needs reproduction Failing test case needed labels Dec 17, 2023
@spawnia
Copy link
Collaborator

spawnia commented Dec 17, 2023

Fixed with https://github.com/nuwave/lighthouse/releases/tag/v6.29.1. Please consider sponsoring.

@spawnia spawnia closed this as completed Dec 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An error within Lighthouse
Projects
None yet
Development

No branches or pull requests

2 participants