Validation Constraint, optional field in DTO?

4.6k Views Asked by At

I have a DTO with a couple fields, username, password and email All of them Strings.

I also have a List that I want as an optional, meaning it does not have to be passed in from the form and can be left blank.

This should be valid from the front-end:

{
"username":"user",
"email":"[email protected]",
"password": "123",
"hobbies":[ "Weightlifting", "Dancing"]
} 

So should this:

{
"username":"user",
"email":"[email protected]",
"password": "123"
} 

Is there any @OptionalField annotation or the likes within javax.validation.constraints?

Or is my only option two seperate DTOs?

1

There are 1 best solutions below

0
Lithicas On

Solution in this case was to make the field an Optional. Example, Optional<Set<Hobby>> hobbies;