It’s imperative to have good data validations in place. Because often, data leads to more bugs even when logic works correctly.
Data errors can be:
- null values in the database or from an external system
- expecting values to be positive by getting negative ones.
- expecting a field to have a specific format but getting something else
The only solution to a data error is running data migrations which is an unnecessary overhead.
Therefore, we should ensure that data is validated before it reaches DB.
Only allows certain data
You minimize data problems by only allowing it for specific fields.
On a UI, it could be dropdowns, date fields with range, and auto-completion options.
Validate on frontend
Validate on the backend
In Java specifically, you have JSR Validators.
Use in all POJOs, especially for incoming request objects and entity objects.
You can also create custom validators.
Using enums is also an option.