Reputation: 11467
We have the situation where we have a Customer and Address domain class like:
Customer:
@NotNull
private Long id;
@NotBlank
@Length(max = 100)
private String name;
@NotNull
@Valid
private Address homeAddress;
@Valid
private Address deliveryAddress;
Address:
@NotBlank
@Length(max = 100)
private String streetname;
@NotBlank
@Length(max = 10)
private String housenumber;
@Length(max = 10)
private String housenumberExtension;
@NotBlank
@Pattern(regexp = "[0-9]{4}[A-Z]{2}")
private String postcode;
@NotBlank
@Length(max = 100)
private String city;
It uses the @Valid annotation to validate the whole Customer graph including the child Address fields.
We have the case where we only want to validate the Address fields in case it has non empty fields. So e.g. in case all customer.homeAddress fields are null it should be ok and should not result in validation errors.
I know it would be better then to make homeAddress null to skip validation. But we are using an old domain model where this is not the case (and we cannot change it). Anyway we are trying to incorporate java.validation annotations.
I wonder if we could apply a (custom) annotation on class level which can force the validation api to skip that bean?
Upvotes: 1
Views: 4706
Reputation: 482
I think apply a custom annotation at Class level is a good way to do this validation. You can see the Hibernate-Validator to find how to do this.
I hope it may help you, Best regards.
Upvotes: 1