Skip to content

Custom collection constraint #1195

@theofidry

Description

@theofidry
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 Symfony\Component\Validator\Constraints\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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions