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.
bun
bun add elysia typebox-standard-schemanpm
npm install elysia typebox-standard-schemaA complete route
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 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 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:
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.