Custom Validator

Learn how to create custom validations that work for the whole object.

We’ve been using the built-in constraints from the javax.validation.constraints package until now.

Creating custom validations

When those are not enough, we can create our own custom validation. Such validation can validate a single field or the whole class. Check out the Custom Validator to check if, say, a String contains an XML blog post as an example of a custom validation on a single field.

We will create a custom validator that validates the whole object. For example, it would be good to validate if there is already a known user with the given email address since the email address of each user should be unique.

Create annotation

We start by creating our own annotation:

Get hands-on with 1200+ tech skills courses.