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.
We start by creating our own annotation: