Custom constraints
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
write a reusable constraint.
Planned topics
- Needs two parts: an annotation and a validator class.
- The annotation carries
message,groups,payload, and@Constraint(validatedBy = ...). - The validator implements
ConstraintValidator<Annotation, Type>withisValid. - Keep it a pure check of the value; no database calls.
Draft outline details
Demonstration
@Target(FIELD) @Retention(RUNTIME)
@Constraint(validatedBy = SkuValidator.class)
public @interface ValidSku {
String message() default "invalid SKU";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}Check your understanding
What are the two parts of a custom constraint?
Show answer
The annotation and the validator.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.