0

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.

3
  • 1
    Is @Valid annotation used wherever you are trying to use MyRequest ? Commented Aug 5, 2022 at 5:03
  • I have added @Valid to the parameter MyRequest myRequest in a method which is getting called in the code. Commented Aug 10, 2022 at 23:18
  • Also, I tried creating another class and added a field to it of type MyRequest, and then used this new class to capture the request of a controller, with @Valid in the controller parameter. There the validation in myRequest are working. But not directly as I mentioned in my previous comment. Commented Aug 10, 2022 at 23:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.