Skip to content
New issue

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

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

Already on GitHub? # to your account

Support Optional as a container in validation module #138

Closed
ldaley opened this issue Sep 15, 2020 · 5 comments · Fixed by #139
Closed

Support Optional as a container in validation module #138

ldaley opened this issue Sep 15, 2020 · 5 comments · Fixed by #139

Comments

@ldaley
Copy link

ldaley commented Sep 15, 2020

As far as I can tell, Optional and the validation module don't play well together. The pattern for validated optionals is given here:

https://docs.jboss.org/hibernate/validator/5.2/reference/en-US/html/ch02.html#_with_literal_java_util_optional_literal

So effectively,

Optional<@Size(min = 10) String> someString

Should be interpreted as:

@Size(min = 10)
@Nullable
String someString
@ldaley
Copy link
Author

ldaley commented Sep 15, 2020

I'm going to need to do this on a current project and will likely start by forking the existing validation module. If I work it out, I'll post what I come up with back here.

@CarstenWickner
Copy link
Member

CarstenWickner commented Sep 15, 2020

@ldaley you might want to look into this issue then: FasterXML/java-classmate#25
That's why those inline annotations are currently not available.

For your particular use-case, this should already have the desired effect (just as a workaround):

@Size(min = 10)
Optional<String> someString;

A similar feature had been requested for collections before, that led me to the topic of AnnotedTypes and a workaround: #60
Maybe that could be extended to Optional somehow.

@CarstenWickner
Copy link
Member

Hi Luke,

I'm happy to let you know, that these kinds of validation annotations are now also supported on Optional.
Similar to #60, this is limited to the top-level, which is hopefully good enough for your use-case.

  • Optional<@Size(min = 5) String> will be picked up
  • List<Optional<@Size(min = 5) String>> will not be picked up

It'll be included in the next release then.

@CarstenWickner
Copy link
Member

Here you go: release v4.15.0 is published.

@ldaley
Copy link
Author

ldaley commented Sep 15, 2020

@CarstenWickner Thank you for the fast turnaround, and excellent library.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants