Junior — Middle
What methods can you name for verifying data correctness?
sobes.tech AI
Answer from AI
The main methods of data validation include:
- Format validation — checking that data conforms to the expected format (e.g., email, phone, date).
- Range checks — numerical values should fall within an acceptable range.
- Required fields check — ensuring all necessary data is filled.
- Uniqueness check — for example, a unique login or identifier.
- Cross-field validation — consistency between different fields (e.g., start date not later than end date).
- Null check — to avoid NullPointerException.
- Regular expressions — for complex pattern validation.
- Business logic validation — for example, access rights or domain restrictions.
In Java, these are often implemented using:
- Built-in checks in code (if-conditions).
- Annotations from the Bean Validation API (e.g., @NotNull, @Size, @Pattern) along with implementations like Hibernate Validator.
Example with Bean Validation:
public class User {
@NotNull
@Size(min = 2, max = 30)
private String name;
@Email
private String email;
// getters and setters
}
This allows for centralized and declarative data validation rules.