Error Encyclopedia
422

422 Unprocessable Entity Error

Learn what 422 Unprocessable Entity means for API validation errors, how it differs from 400, and how to fix validation failures.

What Does This Error Mean?

The 422 Unprocessable Entity status code means the server understands the request syntax but cannot process it due to semantic errors — the request is well-formed but contains invalid data that fails business logic or validation rules.

Common Causes

1

Required field is missing from the request body

2

Field value fails format validation (email, phone, URL)

3

Business rule violation (e.g., duplicate username)

4

Referenced resource does not exist (foreign key violation)

5

Date range is invalid (end before start)

6

Enum value is not in the allowed list

How to Fix It

Read the error response body

422 responses typically include detailed field-level validation errors in the response body.

fetch("/api/users", {
  method: "POST",
  body: JSON.stringify({ email: "invalid" })
}).then(async res => {
  if (res.status === 422) {
    const errors = await res.json()
    // { errors: { email: ["Invalid email format"] } }
    console.log(errors)
  }
})

Validate on the client first

Validate form data before sending to reduce 422 responses.

function validateUser(data) {
  const errors = {}
  if (!data.email?.includes("@")) errors.email = "Invalid email"
  if (!data.name?.trim()) errors.name = "Name is required"
  if (data.age < 0 || data.age > 150) errors.age = "Invalid age"
  return Object.keys(errors).length ? errors : null
}

Check field formats and constraints

Verify each field matches the expected format, length, and type.

// Common validation patterns
{ "email": "user@example.com" }      // ✅ Valid format
{ "email": "not-an-email" }          // ❌ 422
{ "age": 25 }                         // ✅ Valid
{ "age": -5 }                         // ❌ 422 (out of range)

Before & After Examples

❌ Before
POST /api/users
{"email":"bad","name":"John"}
✅ After
POST /api/users
{"email":"john@example.com","name":"John"}

Related Tools

Use these tools to debug and fix this error:

Related Errors

Other common errors in this category: