Skip to content

Validation Errors

Validation errors are the most common errors an API returns, and the ones clients most want to render nicely. A good validation response tells the user exactly which fields are wrong and why — all of them at once, not one per round trip.

When the request parsed correctly but the values fail your rules, return 422 Unprocessable Entity. Some teams use 400 for all client errors; either is defensible, but be consistent. Reserve 400 for requests you could not even parse.

Extend Problem Details with an errors array, one entry per offending field:

{
"type": "https://api.example.com/problems/validation",
"title": "Validation failed",
"status": 422,
"detail": "The request has 2 invalid fields.",
"errors": [
{ "field": "email", "message": "must be a valid email address" },
{ "field": "age", "message": "must be 18 or greater" }
]
}

Validate every field and gather all failures before responding — do not bail on the first. This mirrors how a typical schema validator works:

JavaScript

In TypeScript projects a schema library (such as zod) gives you this for free: parse the input against a schema, and on failure map the issues to your errors array.

Input parses fine but two fields break business rules. Best status?
Why return all validation errors at once instead of the first?
What makes a validation error body easy for clients to render?