Skip to content
Ameen Abozaid
Planned lessonJNR-014-L03

Custom constraints

  • Lesson 3 of 6
  • Junior
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> with isValid.
  • Keep it a pure check of the value; no database calls.
Draft outline details

Demonstration

java
@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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.