'Use validation inside validator or dont validate empty string with another validation or change empty string to null
Having request body as:
"pesel": "",
"something": "BLABLABLA"
I would like to achieve logic as below:
1. If the field is null or is an empty string, then field isValid
2. If the fields lenght is more than 0 then use @PESEL validation
I have tried adding JsonInclude adnotation but it didnt help, @PESEL adnotation throw error with empty string - "":
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@PESEL
private String pesel;
Creating own validator:
@Component
public class PeselCustomValidator implements ConstraintValidator<PESELCustom, String> {
@PESEL
private String pesel;
@Override
public boolean isValid(String pesel, ConstraintValidatorContext constraintValidatorContext) {
if(pesel.equals("")){return true;}
// Many variations like @PESEL in the parameters
// @Valid etc
}
}
Unfortunately nothing is working as expected.
On the other hand I have also tried with validation order and some way to change "" to null in the initBinder but without success. Thank you in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
