---
title: "Validation error"
description: "Learn how to customize validation error messages in Elysia, including providing detailed feedback for invalid inputs."
url: "https://next.elysiajs.com/tutorial/patterns/validation-error/"
---

# Validation error

If you use `t` from `elysia` for validation, you can provide a custom error message based on the field that fails the validation.

```typescript
import { Elysia, t } from 'elysia'

new Elysia()
  .post(
    '/',
    {
      body: t.Object({
        age: t.Number({
          error: 'Age must be a number'
        })
      }, {
        error: 'Body must be an object'
      })
    }, ({ body }) => body
  )
  .listen(3000)
```

Elysia will override the default error message with the custom one you provide, see [Custom validation message](/patterns/error-handling/#custom-validation-message).

## Validation detail

By default Elysia also provides a [Validation detail](/patterns/error-handling/#validation-detail) to explain what’s wrong with the validation as follows:

```json
{
  "type": "validation",
  "code": "validation",
  "title": "Validation Error",
  "status": 422,
  "detail": "must be number",
  "on": "params",
  "property": "/id",
  "found": {
    "id": "string"
  },
  "expected": {
    "id": 0
  },
  "errors": [
    {
      "keyword": "type",
      "schemaPath": "#/properties/id",
      "instancePath": "/id",
      "params": {
        "type": "number"
      },
      "message": "must be number"
    }
  ]
}
```

However, when you provide a custom error message, it will completely override [Validation detail](/patterns/error-handling/#validation-detail)

To bring back the validation detail, you can wrap your custom error message in a [Validation detail](/patterns/error-handling/#validation-detail) function.

```typescript
import { Elysia, t, validationDetail } from 'elysia'

new Elysia()
  .post(
    '/',
    {
      body: t.Object({
        age: t.Number({
          error: validationDetail('Age must be a number')
        })
      }, {
        error: validationDetail('Body must be an object')
      })
    }, ({ body }) => body
  )
  .listen(3000)
```

## Assignment

Provide your own validation message when the request body contains an invalid age.
