Request Validation
Why Validate?
Malicious/invalid input:
- SQL injection: "'; DROP TABLE users;--"
- XSS: "<script>alert('hack')</script>"
- Oversized data: 1GB payload
- Wrong types: string where int expected
- Missing required fields
Validation Layers
Client Input
|
v
[Client-side validation] → Quick feedback
|
v
[API Gateway validation] → Rate limits, content type
|
v
[Controller validation] → Format, required fields
|
v
[Service validation] → Business rules
|
v
[Database constraints] → Data integrity
Validation in Spring Boot
public class CreateProductRequest {
@NotBlank(message = "Name is required")
@Size(min = 2, max = 100)
private String name;
@NotNull(message = "Price is required")
@Positive(message = "Price must be positive")
private BigDecimal price;
@Email(message = "Invalid email format")
private String sellerEmail;
@Pattern(regexp = "^[A-Z]{2}-[0-9]{8}$")
private String productCode;
}
Validation Error Response
{
"error": "VALIDATION_ERROR",
"message": "Request validation failed",
"details": [
{ "field": "name", "message": "Name is required" },
{ "field": "price", "message": "Price must be positive" }
]
}
Validation Best Practices
Validation Layers
- Client-side: Immediate feedback
- API Gateway: Basic validation
- Service: Business rules
- Database: Constraints
Types
- Type checking
- Format validation (email, phone)
- Range checking
- Length limits
- Business rules
Best Practices
- Validate on server (never trust client)
- Return specific error messages
- Use whitelist approach
- Log validation failures
Key Points
- Understanding Request Validation is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for Request Validation in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Request Validation implementation
// Key aspects: validation, error handling, logging, testing
public class RequestValidation {
// Production-ready implementation
}Identify and handle edge cases for Request Validation. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotencyWrite a testing strategy for Request Validation. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. Where should validation primarily happen in the backend?
2. What does @NotBlank validate in Spring?
3. What is the primary purpose of Request Validation?
4. What is a common mistake when implementing Request Validation?
Flashcards
Question
Where to validate in backend?
Click to reveal answer
Answer
Controller/service layer + database constraints as safety net
Question
What does @NotBlank check?
Click to reveal answer
Answer
Not null + not empty + not whitespace only
Question
What is Request Validation?
Click to reveal answer
Answer
Request Validation is a key concept in backend development.
Question
When to use Request Validation?
Click to reveal answer
Answer
Use Request Validation when building production systems that require reliability, scalability, and maintainability.
Question
Request Validation best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1.Validate at multiple layers: client, controller, service, database
- 2.Use annotation-based validation in Spring (@NotBlank, @Email, etc.)
- 3.Always return detailed validation error responses
- 4.Never trust client input — validate everything
Interview Tips
- •Explain the validation pipeline
- •Know Spring validation annotations
Cheat Sheet
Request Validation
- Layers: Client -> Gateway -> Controller -> Service -> DB
- Spring: @NotBlank, @NotNull, @Email, @Pattern, @Size
- Error Response: field + message for each violation
- Rule: Never trust client input