Constraint Validation Guide with Java Custom Annotation
Dominate your data with Basic Validation
Good work to everyone. In today’s article, I will explain how we can prepare Validation operations with java in an ‘optimized, centric and reliable’ and Service independent way. But first I want to talk about the importance of this.
To give an example, you have a program and the program has clients, there is an interface, there is an interaction. You want to get a registration form from the user. The fields you will get are as follows: name, surname, email, refCode, address
Validation is the most important mechanism that protects us from ‘harmful’ inputs. In a system without validation, the user can enter an email in the name section, or a phone number in the email section. All similar scenarios can happen. The prevention integrated to prevent this is called Validation.
We can prevent this by using Java’s own libraries. But as the title of our article suggests, we will do it ourselves. however, before we do it ourselves, I would like to introduce the javax.validation library and some validations.
NotBlank : The annotated element must not be null and must contain at least one non-whitespace character. Accepts CharSequence.
NotNull : The annotated element must not be null. Accepts any type.
Email : The string has to be a well-formed email address. Exact semantics of what makes up a valid email address are left to Jakarta Bean Validation providers. Accepts CharSequence.
Pattern : The annotated CharSequence must match the specified regular expression.
Size : The annotated element size must be between the specified boundaries (included).
Min — Max : The annotated element must be a number whose value must be higher-lower or equal to the specified min-max.
AssertTrue : The annotated element must be true
We can add one last example usage, close this javax topic and talk to about Custom Constraint.
Custom Constraint Preparation
We will do this in an Aspect Oriented way. First, let’s create a Validator Annotation on a global scale.
Then we can write the Validators we reference in the validatedBy section. I will only create NullOrNotBlankValidator for the example.
As can be seen in the code, we have now been able to detect both blank Strings and null values and provide a conditioning according to character lengths. Let’s set the message setting method, which is the common operation of all our Validators, in BaseValidator.
Let’s Use it on Field
We wrote a Custom Annotation, created Validators for this mechanism and implemented it in a completely plug-in way. When you need to validate another Data type, you only need to write the validator and reference the CustomConstraint annotation.
To Explain a Little More
Your Regular expression model that you will give to the regexp field ensures whether that data is validated or not. For example, no one will be able to enter an email in the field named city. Because my regex pattern named BLACK_LIST checks if there is an email in it.
I hope this article will be useful to you. Thank you for reading, I wish you good work.
You can visit my Github Address and Linkedin Profile to support me.