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

Custom collection constraint #1195

Open
theofidry opened this issue Oct 14, 2024 · 0 comments
Open

Custom collection constraint #1195

theofidry opened this issue Oct 14, 2024 · 0 comments

Comments

@theofidry
Copy link
Contributor

Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? no
Version/Branch 1.6.0

As of now constraints can be applied as such:

AddUserInput:
    type: input-object
    config:
        fields:
            username:
                type: String!
                validation: # using an explicit list of constraints
                    - NotBlank: ~
                    - Length: { min: 6, max: 32 }
            password:
                type: String!
                validation: App\Entity\User::$password # using a link
            email:
                type: String!
                validation: # mixing both
                    link: App\Entity\User::$email
                    constraints:
                        - Email: ~

User:
    type: input-object
    config:
        validation:
            - Callback: [App\Validation\UserValidator, validate]
        fields:
            username:
                type: String!
            password:
                type: String!
            email:
                type: String!

Maybe it is more of a personal thing but I find easier to sometimes define a custom constraint (potentially with its own validator) in plain PHP code. In other words, I have a Collection constraint AddUserInput, which I validate in my resolver:

$violations = $this->validator->validate($input, new AddUserInput());

if (count($violations) > 0) {
    throw new ArgumentsValidationException($violations);
}

And whilst it works, I wish I could just do this:

AddUserInput:
    type: input-object
    config:
        validation
            - App\GraphQL\Validation\AddUserInput: ~
        fields:
            username:
                type: String!
            password:
                type: String!
            email:
                type: String!

Current this will not work because it is a Collection constraint which cannot be applied to a class. However in usage, it does not really need to be a class. Do you have any idea how we could make this work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant