Skip to main content
Version: 4.x

Validation failed

Type URIhttps://doc.expresso-ts.com/docs/errors/validation-failed
HTTP status400 or 422

A value in the request, usually its body, broke a rule of the schema or class it is validated against.

When it is sent​

The framework validated the request. A handler names a schema, for example @validatedBody(CreateUserSchema) in a controller or validateBody(CreateUserSchema) in front of a micro route, and the value does not pass. The status is 400 and the code is EXP3003. Each failure is an entry in errors.

{
"type": "https://doc.expresso-ts.com/docs/errors/validation-failed",
"title": "Validation Failed",
"status": 400,
"detail": "2 validation error(s) occurred",
"instance": "/api/users",
"errorCode": "EXP3003",
"errors": [
{ "field": "email", "message": "Invalid email address" },
{ "field": "name", "message": "Too small: expected string to have >=1 characters" }
]
}

The messages come from the validation library the schema is written in. The members of each entry depend on the format the application chose in addValidation(): the default has field and message, and the rfc7807 format has pointer, title and code.

The application validated something itself. A handler throws AppError.validationFailed(errors) or new ValidationError(errors). The status is 422 and the failures are in validationErrors.

What to do​

Correct the fields named in errors and send the request again. The request was understood; only its values were refused.

See Error responses for the shape of the document and how to turn it on.