I have a model object annotated with Java validation annotations.
class Criteria{
@NotEmpty String id;
@NotEmpty String name;
String fileName;
String hours;
}
As per the business requirement, either fileName or hours field will be populated, to validate which I have a custom validator 'CriteriaValidator' written.
@Documented
@Constraint(validatedBy = {CriteriaValidator.class})
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE , ElementType.FIELD, ElementType.PARAMETER})
public @interface ValidCriteria {
String message() default "{Criteria.Invalid}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
The model above is passed in as input parameter for a method which is also annotated with @Valid and @ValidCriteria.
public void update(@Valid @ValidCriteria Criteria criteria){...}
The issue I have is with 2 annotations in the method. I have to add @Valid in order to check constraints annotated within the class. My custom validator checks only for the existence of one of the 2 fields: filename or hours.
The question I have is, how do I have a single annotation @ValidCriteria which validates my special requirement as well as the @NotEmpty fields in the object.
I tried adding @Valid in my custom constraint @ValidCriteria, however I get the error '@Valid is not applicable to annotation type'