---
title: "Elysia with TypeBox 1.x Standard Schema"
sidebar: "Elysia"
description: "Validate Elysia bodies, queries and responses with TypeBox 1.x Standard Schema, strip writeOnly fields from responses, and get OpenAPI with no converter."
---

# Elysia

Elysia accepts a Standard Schema in every validator slot (`body`, `query`, `params`, `headers`) and checks the response too. A schema built with this package's `Type` plugs in directly, and Elysia's OpenAPI plugin reads its JSON Schema without a converter.

<!-- tabs -->

**bun**

```sh
bun add elysia typebox-standard-schema
```

**npm**

```sh
npm install elysia typebox-standard-schema
```

<!-- /tabs -->

## A complete route

```ts
import { Elysia } from 'elysia'
import { Type } from 'typebox-standard-schema'

const User = Type.Object({
  id: Type.Readonly(Type.Integer()),
  email: Type.String({ format: 'email' }),
  password: Type.Writeonly(Type.String({ minLength: 8 })),
})

const Search = Type.Object({
  q: Type.String({ minLength: 1 }),
  page: Type.Integer({ minimum: 1, default: 1 }),
})

new Elysia()
  .post('/users', ({ body }) => db.insertUser(body), {
    body: Type.Write(User), // body: { email: string, password: string }
    response: Type.Read(User), // a returned password is stripped
  })
  .get('/users', ({ query }) => db.searchUsers(query.q, query.page), {
    query: Search, // page: number, 1 when absent
  })
  .listen(3000)
```

`db` stands for your data layer. The request body is validated against `Type.Write(User)`, so a client can't set `id`: if it sends one, the field is dropped before the handler runs. The handler returns the whole row, and Elysia validates it against `Type.Read(User)`, which strips `password` from the response and types the handler's return value.

A cut field that is present is still [checked against its own schema](/typebox-standard-schema/validation.md#pipeline) before it goes. A stored bcrypt hash passes `minLength: 8`, but a `maxLength` or `pattern` written for the plaintext password would reject the row; leave the column out of the row in that case.

Query strings arrive as strings; [coercion](/typebox-standard-schema/validation.md) turns `page=2` into `2`, and the default fills it in when absent.

## Invalid input

An invalid request is answered with `422`. The body lists the issues, each with its `path`, so a client can mark the fields that failed.

## OpenAPI

`@elysiajs/openapi` reads `~standard.jsonSchema` on its own:

```ts
import { openapi } from '@elysiajs/openapi'

new Elysia()
  .use(openapi())
  .post('/users', ({ body }) => db.insertUser(body), {
    body: Type.Write(User),
    response: Type.Read(User),
  })
```

The request body shows up without `id` and the `200` response without `password`. More on targets and options in [JSON Schema & OpenAPI](/typebox-standard-schema/json-schema.md).
