Skip to content
beginner Phase · REST API Development

Request Validation

Validate incoming API requests to ensure data integrity.

40m
0 problems
Topic Progress 0%

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

  1. Client-side: Immediate feedback
  2. API Gateway: Basic validation
  3. Service: Business rules
  4. 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

  1. Validation: Always validate input at the boundary
  2. Error Handling: Use structured error responses
  3. Logging: Log key events for debugging
  4. Testing: Unit, integration, and load tests
  5. Documentation: Keep docs updated with code changes

Practice Problems

0 / 3 solved
Implement Request Validation

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
}
Request Validation Edge Cases

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, idempotency
Request Validation Testing Strategy

Write 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 injection

Quiz

1. Where should validation primarily happen in the backend?

Question 1 options

2. What does @NotBlank validate in Spring?

Question 2 options

3. What is a common mistake when implementing Request Validation?

Question 3 options

Flashcards

Question

Where to validate in backend?

Answer

Controller/service layer + database constraints as safety net

Question

What does @NotBlank check?

Answer

Not null + not empty + not whitespace only

Question

Request Validation best practices

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