I'm trying to validate a DTO based on a method using @javax.validation.AssertTrue
annotation.
@Getter
@Setter
public class EntityDTO {
@NotNull
@Schema(...)
private String someField;
@Schema(...)
private String firstName;
@Schema(...)
private String lastName;
@AssertTrue(message = "either first or last name must be set")
private boolean isFirstNameOrLastNameSet() {
return (firstName != null || lastName != null)
}
}
If I validate this class (using @Valid) where first and lastName is null like this:
save(@Valid EntityDTO);
I get this error:
java.lang.IllegalStateException MESSAGE: JSR-303 validated property
'firstNameOrLastNameSet' does not have a corresponding accessor for Spring data binding
- check your DataBinder's configuration (bean property versus direct field access)
I don't want to change data binding globally because I don't know all the implications it has on other code. (Like suggested here How to configure direct field access on @Valid in Spring?)
I was expecting this to work similarly to a normal @NotNull annotation (it works as expected on someField
) . Where it gets validated, and if it fails I get an Exception. Is there a way to achieve this behaviour or am I expecting the wrong thing? How would you validate that at a certain point in the code the isFirstNameOrLastNameSet()
returns true?