1

I have two objects:

public class CancelRequest{
 @NotEmpty
 private String id;

 @NotNull
 private BookingDetails;
}

BookingDetails object:

public class BookingDetails{
 @NotEmpty
 private String bookingId;
}

My class that is responsible for validating the request is CancelRequestValidator

...
import javax.validation.Validator;
...
public class CancelRequestValidator{
...
public void check(CancelRequest request){
 Set<ConstraintViolation<NorthAmericaCancelTripRequest>> violations = 
validator.validate(request);
...
 }
}

how I am performing validation?

CancelRequest request = new CancelRequest("id",new BookingDetails(""));
CancelValidator validator = new CancelValidator();
violations = validator.check(request);

The violation should have one entry in it saying bookingId cannot be empty but instead it thinks that the object valid. I need to do the validation this way because I have some other business validation that I need to perform on this object hence I am using a mixture of javax and custom validation.

thanks

2 Answers 2

3

To perform nested bean validation, you need to use the @Valid annotation on the nested object:

public class CancelRequest{
  @NotEmpty
  private String id;

  @NotNull
  @Valid
  private BookingDetails;
}
Sign up to request clarification or add additional context in comments.

Comments

0

As Eamon Scullion said, @Valid will work for nested validations, dont forget though to use @NotNull for Objects like Date, @NotBlank for text fields, and @NotEmpty for Iterable objects

public class CancelRequest{
    @NotBlank
    private String id;
    

    @NotEmpty
    private List<E> myList;

    @NotNull
    @Valid
    private BookingDetails;
}

Comments

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.