Consistent API Validation Errors: RFC 9457 and Field Error Contracts
Use the HTTP status to show the broad failure class and a stable machine-readable body with field-level information the client can correct. RFC 9457 Problem Details can provide a common outer format, with field errors as API-defined extensions; it is a standard option, not mandatory for every API.
Use the HTTP status to show the broad failure class and a stable machine-readable body with field-level information the client can correct. RFC 9457 Problem Details can provide a common outer format, with field errors as API-defined extensions; it is a standard option, not mandatory for every API.
This covers input validation in HTTP JSON APIs. RFC 9457 (2023) obsoletes RFC 7807. A 422 response means the server understands the content type and syntax but cannot process the instructions. Keep an existing API’s documented status policy unless compatibility is planned.
Define the contract
**Choose status by failure kind.** Separate malformed/unreadable content, semantically invalid input, authentication or authorization failures, and resource conflicts. 422 may fit syntactically valid but unprocessable content. The 400/422 choice is a contract decision; define one consistent policy.
**Keep a stable problem shape.** Use `application/problem+json`. `type` identifies the problem, `title` gives a stable short name, `status` matches the HTTP status, `detail` explains this occurrence, and `instance` can identify it. Clients should branch on `type` or documented fields, not parse prose in `detail`.
**Define field errors as an extension.** RFC 9457 illustrates validation errors as an extension, but each API chooses names and structure. This synthetic example uses JSON Pointer locations and stable codes:
{
"type":"https://api.example.com/problems/validation-error",
"title":"Some values are invalid","status":422,
"detail":"One or more fields need correction.",
"instance":"/problems/occurrences/01JEXAMPLE",
"errors":[
{"pointer":"#/email","code":"invalid_format"},
{"pointer":"#/age","code":"must_be_at_least","minimum":18}
]
}Pair it with Content-Type: application/problem+json. Decide whether to use JSON Pointer or another location format and how to represent multiple issues. Match pointers to the actual request shape.
**Document and verify endpoints.** OpenAPI should describe 4xx statuses, media type, schema, and extensions. Include missing, malformed, out-of-range, and multiple-error cases. Check status, Content-Type, body shape, location, and stable code; confirm framework defaults produce the same contract on every route.
Consistency and safety
Keep codes stable and treat title/detail as display text. Explain how to fix input without returning stack traces, SQL, internal paths, secrets, or full submitted data. Correlate diagnostics through request IDs and redact sensitive values. Accept-Language may localize human-readable text while status, code, location, and conditions stay unchanged. Do not force Problem Details where a status or existing resource format is sufficient; check compatibility before changing a published contract.
A useful error contract lets clients choose recovery from status and stable codes. Agree on failure categories, define the common body and field-error extension, and align OpenAPI and tests.\n\n## References\n- RFC 9457: Problem Details for HTTP APIs\n- RFC 9110: HTTP Semantics\n- OpenAPI Specification 3.2.1