Home
/
API Response Examples
/
Validation error
Validation error JSON response example
Last updated 2026-05-20
When to use this
Use this for a 422 validation-error envelope — per-field error messages, a top-level message, and an error code your client can branch on.
Example JSON
POST /users/add with a bad payload returns this shape.
{
"message": "Validation failed",
"code": "validation_failed",
"errors": {
"email": [
"Email is required",
"Email must be valid"
],
"password": [
"Password must be at least 8 characters"
],
"age": [
"Age must be a positive integer"
]
}
}
Request examples
fetch()
cURL
Copy
const res = await fetch('https://jsonexamples.com/users/add', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: '', password: '123', age: -1 })
});
if (!res.ok) {
const { errors } = await res.json();
console.error(errors);
}
Copy
curl -s -X POST https://jsonexamples.com/http/422/Validation+failed
Try the live endpoint
Click below to call /http/422/Validation+failed from your browser.
Call /http/422/Validation+failed
// click the button to populate this block
Common variations
RFC 7807 problem+json
Equivalent error in the IETF problem-details format.
{
"type": "https://jsonexamples.com/errors/validation-failed",
"title": "Validation failed",
"status": 422,
"detail": "Three fields failed validation.",
"instance": "/users/add",
"errors": [
{
"field": "email",
"message": "Email is required"
},
{
"field": "password",
"message": "Password must be at least 8 characters"
}
]
}
Convert this JSON
Generated starting points for TypeScript, JSON Schema, and Zod. Refine before shipping to production.
TypeScript
JSON Schema
Zod
Copy
export interface ValidationError {
"message": string;
"code": string;
"errors": {
"email": string[];
"password": string[];
"age": string[];
};
}
Copy
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ValidationError",
"type": "object",
"properties": {
"message": {
"type": "string"
},
"code": {
"type": "string"
},
"errors": {
"type": "object",
"properties": {
"email": {
"type": "array",
"items": {
"type": "string"
}
},
"password": {
"type": "array",
"items": {
"type": "string"
}
},
"age": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
"email",
"password",
"age"
],
"additionalProperties": false
}
},
"required": [
"message",
"code",
"errors"
],
"additionalProperties": false
}
Copy
import { z } from 'zod';
export const ValidationErrorSchema = z.object({
message: z.string(),
code: z.string(),
errors: z.object({
email: z.array(z.string()),
password: z.array(z.string()),
age: z.array(z.string())
})
});
export type ValidationError = z.infer<typeof ValidationErrorSchema>;
Copy link to this example
https://jsonexamples.com/api-response-examples/validation-error