Skip to content

Feature Request: Support for list elements #31

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

Closed
pelletier197 opened this issue Sep 30, 2020 · 3 comments · Fixed by #48
Closed

Feature Request: Support for list elements #31

pelletier197 opened this issue Sep 30, 2020 · 3 comments · Fixed by #48

Comments

@pelletier197
Copy link
Contributor

It would be great to have support of validation for list items.

For instance with this query schema

            type Query {
                cars(targets: [String!]! @NotBlank) : [Car]
            }

The @NotBlank could validate that every element of the list is not blank. This applies indeed for all the validations, except maybe for the expression, which could still be useful to use on a full list

@hantsy
Copy link

hantsy commented Sep 29, 2021

For a list, it should be @NotEmpty to check if there is an item in the collection.

@hantsy
Copy link

hantsy commented Sep 29, 2021

@NotBlank in bean validation is generally used to check if it is a empty string or a string only contained whitespace in the string.

@pelletier197
Copy link
Contributor Author

pelletier197 commented Sep 29, 2021

Technically speaking, javax supports both collections and strings. https://javaee.github.io/javaee-spec/javadocs/javax/validation/constraints/NotEmpty.html. NotEmpty will allow whitespace characters, while NotBlank will ensure you have at least one non whitespace char.

But the way you can validate a list or the elements of a list with javax is as follow:

// list not empty 
public @NotEmpty List<String> field;

// strings in the list not empty
public List<@NotEmpty String> field;

The equivalent syntax does not compile in GraphQL.

// works 
field: [String] @NotEmpty

// does not work
field: [String @NotEmpty ]

Thus making it impossible to validate that strings are not empty and that lists are not empty at the same time. Same issue occurs with @Size.

In the linked MR, I came up with @ContainerNotEmpty and @ContainerSize to counter that issue. So far, I have got no news from the maintainers, but if you have a better idea to counter the issue, I'll be happy to change it.

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