I have a request DTO, with nested objects as members, and one of them has an @Email validation on it.
@AllArgsConstructor
@NoArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
public class MyRequest {
@NotNull
@ApiModelProperty(required = true)
@Valid
private MyContact myContact;
}
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@ToString
public class MyContact {
@Valid
private ContactDto representative;
@Valid
private ContactDto owner;
}
@Data
@NoArgsConstructor
@EqualsAndHashCode
@Setter
public class ContactDto {
@NotNull
@ApiModelProperty(required = true, example = "[email protected]")
@Size(max = 255)
@Email(message = "My error message")
protected String email;
}
MyRequest is being used to take the response body from a controller.
@ApiParam(required = true, name = "My Request",
value = "My value")
@Valid @RequestBody MyRequest myRequest
The email validation works fine with the controller.
However, I am using the objects of MyRequest at other places as well (non-controllers), where the validation is not done when an object is created with an invalid email.
MyRequest myRequestin a method which is getting called in the code.