---
title: "Validation"
description: "Check requests and responses with schemas that also give your handlers their TypeScript types."
url: "https://next.elysiajs.com/essential/validation/"
---

# Validation

Elysia supports declarative schema validation.

Describe what your data should look like and Elysia will handle the rest.

## Start with a parameter

Without a schema, `/id/:id` captures a string.

- `/id/42` gives the handler `"42"`
- `/id/hello` gives it `"hello"`

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

new Elysia()
    .get('/id/:id', ({ params: { id } }) => id)
    .listen(3000)
```

## Add a schema

By adding `t.Number()` to the `params` schema, Elysia will convert a numeric path value and validate it.

- `/id/42` gives the handler the number `42`
- `/id/hello` fails validation with status `422`

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

new Elysia()
    .get('/id/:id', {
        params: t.Object({
            id: t.Number()
        })
    }, ({ params: { id } }) => id)
    .listen(3000)
```

## Validate another field

Add `query` to require a `name` query parameter too.

- `/id/42?name=Elysia` returns `{ id: 42, name: 'Elysia' }`.
- `/id/42` fails validation because `name` is missing.

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

new Elysia()
    .get('/id/:id', {
        params: t.Object({
            id: t.Number()
        }),
        query: t.Object({
            name: t.String()
        })
    }, ({ params: { id }, query: { name } }) => ({ id, name }))
    .listen(3000)
```

## Check the response

Add `response` to enforce the shape of the data returned by the handler.

Elysia would also clean up any extra fields in the response, if the handler returned them.

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

new Elysia()
    .get('/id/:id', {
        params: t.Object({
            id: t.Number()
        }),
        query: t.Object({
            name: t.String()
        }),
        response: t.Object({
            id: t.Number(),
            name: t.String()
        })
    }, ({ params: { id }, query: { name } }) => ({ id, name }))
    .listen(3000)
```

## TypeBox

Elysia provides `t`, a named export from `elysia`, as a default schema builder. It’s a wrapper around [TypeBox](https://github.com/sinclairzx81/typebox) with extra features for API validation.

These schemas support runtime checks, TypeScript inference, and schema generation for [OpenAPI](/plugins/openapi/). You don’t need to write a separate interface for the same data.

## Standard Schema

You can also use a library that implements [Standard Schema](https://github.com/standard-schema/standard-schema), including compatible versions of **Zod**, **Valibot**, ArkType, Effect Schema, Yup, and Joi and use them interchangeably.

```ts
import { Elysia } from 'elysia'
import { z } from 'zod'
import * as v from 'valibot'

new Elysia()
    .get('/id/:id', {
        params: z.object({
            id: z.coerce.number()
        }),
        query: v.object({
            name: v.literal('Lilith')
        })
    }, ({ params: { id }, query: { name } }) => ({ id, name }))
    .listen(3000)
```

`/id/42?name=Lilith` passes both schemas. Use your chosen library’s coercion API when you want to turn a URL string into another type.

## Schema type

Choose the property that matches the data you want to check:

### [Body schema](#body)

[Body checks the parsed HTTP request body, such as JSON or form fields.](#body)

### [Query schema](#query)

[Query checks the query string after `?` in the URL.](#query)

### [Params schema](#params)

[Params checks values captured from the route’s path.](#params)

### [Headers schema](#headers)

[Headers checks incoming request headers.](#headers)

### [Cookie schema](/patterns/cookie/#cookie-schema)

[Cookie checks incoming cookie values.](/patterns/cookie/#cookie-schema)

### [Response schema](#response)

[Response checks data returned by the handler.](#response)

Provide these properties in the route’s second argument, before its handler:

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

new Elysia()
    .get('/id/:id', {
        query: t.Object({ name: t.String() }),
        params: t.Object({ id: t.Number() })
    }, () => 'Hello World!')
    .listen(3000)
```

| URL                  | Query schema | Params schema |
| -------------------- | ------------ | ------------- |
| `/id/a`              | Fails        | Fails         |
| `/id/1?name=Elysia`  | Passes       | Passes        |
| `/id/1?alias=Elysia` | Fails        | Passes        |
| `/id/a?name=Elysia`  | Passes       | Fails         |
| `/id/a?alias=Elysia` | Fails        | Fails         |

Both schemas must pass for the handler to run. The table describes each check independently; the error response may report the first failing part.

## Body

An [HTTP Message](https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages) body. It can be JSON, form fields, or a file.

<!-- Keep code outside React SSR, which strips Expressive Code's stylesheet link. -->

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

new Elysia()
    .post('/body', {
        body: t.Object({ name: t.String() })
    }, ({ body }) => body)
    .listen(3000)
```

<!--astro:end-->

| JSON body               | Validation |
| ----------------------- | ---------- |
| `{ "name": "Elysia" }`  | Passes     |
| `{ "name": 1 }`         | Fails      |
| `{ "alias": "Elysia" }` | Fails      |
| No body                 | Fails      |

### File

Use [t.File()](/patterns/typebox/#file) for a single file and [t.Files()](/patterns/typebox/#files) for multiple files.

A `type` check needs a file type detector. Install `file-type` and register it once with `setFileTypeDetector`; without a detector, every upload checked against `type` fails validation.

```ts
import { Elysia, t, setFileTypeDetector } from 'elysia'
import { fileTypeFromBlob } from 'file-type'

setFileTypeDetector(fileTypeFromBlob)

new Elysia()
    .post('/body', {
        body: t.Object({
            file: t.File({ type: 'image/*' }),
            multipleFiles: t.Files()
        })
    }, ({ body }) => ({
        file: body.file.name,
        count: body.multipleFiles.length
    }))
    .listen(3000)
```

### File (Standard Schema)

Standard Schema libraries don’t automatically get Elysia’s file checks. Add a refinement using the asynchronous `fileType()` helper and explicitly select multipart parsing:

```ts
import { Elysia, fileType, setFileTypeDetector } from 'elysia'
import { fileTypeFromBlob } from 'file-type'
import { z } from 'zod'

setFileTypeDetector(fileTypeFromBlob)

new Elysia()
    .post('/body', {
        parse: 'formdata',
        body: z.object({
            file: z.file().refine(
                (file) => fileType(file, 'image/jpeg'),
                'Expected a JPEG file'
            )
        })
    }, ({ body }) => ({ name: body.file.name }))
```

The registered detector supplies the byte check here too. A validator that only reads `file.type` doesn’t establish what the uploaded file actually contains.

## Query

Query parameters follow `?` in a URL. Describe them with an object schema:

<!-- Keep code outside React SSR, which strips Expressive Code's stylesheet link. -->

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

new Elysia()
    .get('/query', {
        query: t.Object({ name: t.String() })
    }, ({ query }) => query)
    .listen(3000)
```

<!--astro:end-->

| Request                             | Validation                 |
| ----------------------------------- | -------------------------- |
| `/query?name=Elysia`                | Passes                     |
| `/query?name=1`                     | Passes; `name` is a string |
| `/query?alias=Elysia`               | Fails                      |
| `/query?name=ElysiaJS&alias=Elysia` | Passes                     |
| `/query`                            | Fails                      |

### Coercion

Elysia converts applicable TypeBox schemas on `query` automatically. For example, `t.Number()` accepts a numeric string and gives the handler a number:

<!-- Keep code outside React SSR, which strips Expressive Code's stylesheet link. -->

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

new Elysia()
    .get('/query', {
        query: t.Object({ id: t.Number() })
    }, ({ query: { id } }) => id)
    .listen(3000)
```

<!--astro:end-->

| Request          | Result                         |
| ---------------- | ------------------------------ |
| `/query?id=1`    | Number `1`                     |
| `/query?id=salt` | Validation error, status `422` |

See [Elysia’s type behavior](/patterns/typebox/#elysia-behavior) for other automatic conversions.

### Array

Declare an array schema to validate each value and support comma-separated input:

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

new Elysia()
    .get('/query', {
        query: t.Object({
            name: t.Array(t.String()),
            squad: t.String()
        })
    }, ({ query }) => query)
    .listen(3000)
```

Both requests below produce `{ name: ['rapi', 'anis', 'neon'], squad: 'counter' }` with this schema.

#### nuqs format

Use commas to separate values, as in the [nuqs](https://nuqs.47ng.com) array format:

```text
/query?name=rapi,anis,neon&squad=counter
```

The array schema tells Elysia to split `name`. Without one, a comma-separated value remains a string.

#### HTML form format

Repeat a key to send several values, as an HTML form does for inputs with the same name:

```text
/query?name=rapi&name=anis&name=neon&squad=counter
```

Declare the array schema to retain and validate every repeated value. Without it, an ordinary query property is a single string; a repeated key keeps its last value.

## Params

Path parameters capture values from a route such as `/id/:id`. Use an object schema to validate the captured fields:

<!-- Keep code outside React SSR, which strips Expressive Code's stylesheet link. -->

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

new Elysia()
    .get('/id/:id', {
        params: t.Object({ id: t.Number() })
    }, ({ params }) => params)
```

<!--astro:end-->

| Request | Result                         |
| ------- | ------------------------------ |
| `/id/1` | `{ "id": 1 }`                  |
| `/id/a` | Validation error, status `422` |

## Headers

Use `headers` to require particular incoming headers:

<!-- Keep code outside React SSR, which strips Expressive Code's stylesheet link. -->

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

new Elysia()
    .get('/headers', {
        headers: t.Object({ authorization: t.String() })
    }, ({ headers }) => headers.authorization)
```

<!--astro:end-->

Header schemas allow additional properties by default. Your client can still send headers such as `accept`; the fields you’ve declared must satisfy their schemas.

If you want to reject any extra headers, set `additionalProperties: false`:

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

new Elysia()
    .get('/headers', {
        headers: t.Object(
            { authorization: t.String() },
            { additionalProperties: false }
        )
    }, ({ headers }) => headers.authorization)
```

## Response

Use `response` to check the data returned by your handler:

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

new Elysia()
    .get('/response', {
        response: t.Object({ name: t.String() })
    }, () => ({ name: 'Jane Doe (ZZZ)' }))
```

TypeScript checks the handler’s return type, and Elysia validates the response at runtime.

### Response per status

Map status codes to schemas when different outcomes have different response shapes:

<!-- Keep code outside React SSR, which strips Expressive Code's stylesheet link. -->

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

new Elysia()
    .get('/response', {
        response: {
            200: t.Object({ name: t.String() }),
            400: t.Object({ error: t.String() })
        }
    }, ({ query, status }) => {
        if (query.fail === 'true')
            return status(400, { error: 'Something went wrong' })

        return { name: 'Jane Doe (ZZZ)' }
    })
```

<!--astro:end-->

`/response` returns the `200` shape. `/response?fail=true` returns the `400` shape. Use `status()` to preserve the relationship between the status code and its body type.

## Validation errors

Customize a schema’s [validation message](/patterns/error-handling/#schema-messages), handle failures with an [error hook](/patterns/error-handling/#error-hook), or inspect the [error list](/patterns/error-handling/#error-list).

For named schemas shared across routes and OpenAPI documentation, see [reference models](/patterns/openapi/#reference-model).

## TypeScript

Use `UnwrapSchema` to get a schema’s inferred TypeScript type:

```ts
import { t, type UnwrapSchema } from 'elysia'

const MyType = t.Object({
    hello: t.Literal('Elysia')
})

type MyType = UnwrapSchema<typeof MyType>
```

A schema can supply runtime validation, coercion, TypeScript types, and OpenAPI metadata. Keep the definition in one place, and let each use follow it.

## Guard

Use `.guard()` to apply a schema to several routes. It affects routes registered after it:

<!-- Keep code outside React SSR, which strips Expressive Code's stylesheet link. -->

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

new Elysia()
    .get('/none', () => 'hi')
    .guard({
        query: t.Object({ name: t.String() })
    })
    .get('/query', ({ query: { name } }) => name)
    .listen(3000)
```

<!--astro:end-->

| Request         | Result                         |
| --------------- | ------------------------------ |
| `/none`         | `hi`                           |
| `/none?name=a`  | `hi`                           |
| `/query`        | Validation error, status `422` |
| `/query?name=a` | `a`                            |

For the same schema property, a later guard takes precedence over an earlier one. A route’s own schema takes precedence over an inherited guard schema.

### Guard schema type

Guards support two ways to handle overlapping schemas.

#### Override (default)

The nearer schema replaces the inherited schema for that property. For example, a route’s `body` replaces a guard’s `body`; it doesn’t merge their fields.

<!-- Keep code outside React SSR, which strips Expressive Code's stylesheet link. -->

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

new Elysia()
    .guard({ body: t.Object({ name: t.String() }) })
    .post('/age', {
        body: t.Object({ age: t.Number() })
    }, ({ body }) => body)
```

<!--astro:end-->

`{ "age": 18 }` satisfies `/age` without a `name` field.

#### Merge

Set `schema: 'merge'` when both the guard and route schemas must be satisfied:

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

new Elysia()
    .guard({
        schema: 'merge',
        response: t.Object({ title: t.String() })
    })
    .get('/article', {
        response: t.Object({ title: t.String(), author: t.String() })
    }, () => ({ title: 'Hello Elysia', author: 'Nagisa' }))
```

The response must satisfy the guard and the route schema. Merge accumulates their constraints instead of replacing the inherited schema.
