Property Validation Techniques
Validation Strategies Overview
graph TD
A[Property Validation] --> B[Basic Checks]
A --> C[Complex Validation]
A --> D[Custom Validation Logic]
A --> E[Annotation-Based Validation]
Basic Validation Techniques
Simple Validation Example
public class User {
private String email;
public void setEmail(String email) {
if (email == null || !email.contains("@")) {
throw new IllegalArgumentException("Invalid email format");
}
this.email = email;
}
}
Validation Pattern Approaches
Validation Type |
Description |
Implementation Strategy |
Null Check |
Prevent null values |
Simple null validation |
Format Validation |
Ensure correct format |
Regex or pattern matching |
Range Validation |
Limit value ranges |
Comparison checks |
Complex Validation |
Multi-condition checks |
Composite validation logic |
Advanced Validation Techniques
Annotation-Based Validation
public class Account {
@NotNull(message = "Username cannot be null")
@Size(min = 3, max = 50, message = "Username must be between 3 and 50 characters")
private String username;
@Pattern(regexp = "^[A-Za-z0-9+_.-]+@(.+)$", message = "Invalid email format")
private String email;
}
Custom Validation Logic
public class PaymentDetails {
private double amount;
public void setAmount(double amount) {
validateAmount(amount);
this.amount = amount;
}
private void validateAmount(double amount) {
if (amount < 0) {
throw new IllegalArgumentException("Amount cannot be negative");
}
if (amount > 1000000) {
throw new IllegalArgumentException("Amount exceeds maximum limit");
}
}
}
Validation Frameworks
Bean Validation (JSR 380)
public class Employee {
@NotBlank(message = "Name is required")
@Size(min = 2, max = 100, message = "Name length must be between 2 and 100")
private String name;
@Positive(message = "Salary must be positive")
private double salary;
}
Validation Best Practices
- Validate at the boundary
- Use consistent error handling
- Provide clear error messages
- Combine multiple validation strategies
- Consider performance implications
Complex Validation Example
public class PasswordValidator {
public boolean isValid(String password) {
return hasMinimumLength(password) &&
containsUppercase(password) &&
containsLowercase(password) &&
containsSpecialCharacter(password);
}
private boolean hasMinimumLength(String password) {
return password != null && password.length() >= 8;
}
private boolean containsUppercase(String password) {
return password.matches(".*[A-Z].*");
}
private boolean containsLowercase(String password) {
return password.matches(".*[a-z].*");
}
private boolean containsSpecialCharacter(String password) {
return password.matches(".*[!@#$%^&*()].*");
}
}
LabEx Validation Recommendations
At LabEx, we emphasize creating robust validation strategies that:
- Ensure data integrity
- Provide clear feedback
- Minimize security risks
- Maintain clean, readable code
Key Validation Principles
- Validate early and often
- Use declarative validation when possible
- Create reusable validation logic
- Handle edge cases comprehensively