typebox-standard-schema
Health Pass
- License — License: MIT
- Description — Repository has a description
- Active repo — Last push 0 days ago
- Community trust — 11 GitHub stars
Code Pass
- Code scan — Scanned 12 files during light audit, no dangerous patterns found
Permissions Pass
- Permissions — No dangerous permissions requested
No AI report is available for this listing yet.
Standard Schema for TypeBox: use TypeBox schemas in tRPC, Hono, Elysia and TanStack Form, with read/write projections
typebox-standard-schema
typebox-standard-schema makes every TypeBox 1.x schema a Standard Schema validator, so it works directly in tRPC, Hono, Elysia, TanStack Form, React Hook Form, SvelteKit remote functions and the other tools that accept Zod or Valibot, with no adapter. From one schema it also derives the request and response shapes, using JSON Schema readOnly and writeOnly, and it emits clean JSON Schema for OpenAPI, AI SDK tools and MCP.
Documentation · Getting started · Framework integrations · API
Installation
bun add typebox-standard-schema
# or
npm install typebox-standard-schema
TypeBox 1.x comes with the package: import Type and everything else from typebox-standard-schema, not from typebox.
Quick start
One schema describes a user; Type.Write and Type.Read derive the request and the response from it. Here with Hono:
import { sValidator } from '@hono/standard-validator'
import { Hono } from 'hono'
import { parse, Type } from 'typebox-standard-schema'
const User = Type.Object({
id: Type.Readonly(Type.Integer()), // set by the server
email: Type.String({ format: 'email' }),
password: Type.Writeonly(Type.String({ minLength: 8 })), // never sent back
})
const users: Array<{ id: number, email: string, password: string }> = []
const app = new Hono()
app.post('/users', sValidator('json', Type.Write(User)), (c) => {
const input = c.req.valid('json') // { email: string, password: string }
const row = { id: users.length + 1, ...input }
users.push(row)
return c.json(parse(Type.Read(User), row), 201) // { id, email }
})
export default app
A client-sent id is dropped before the handler runs, and the password never leaves the server: at runtime and in the types. The array stands in for a database.
Works with
Every schema carries ~standard, so any tool that accepts a Standard Schema takes it as is. Each of these was tested against a project made with the framework's own starter:
- Servers: Hono, tRPC, Elysia, oRPC, h3 / Nitro, Fastify
- Forms and full-stack: SvelteKit remote functions, TanStack Form, React Hook Form, TanStack Router, next-safe-action, T3 Env, Nuxt UI
- JSON Schema consumers: OpenAPI generators, the AI SDK and the MCP SDK, through Standard JSON Schema
Features
- Standard Schema out of the box: no adapter, no compile step;
StandardSchemaV1.InferInput/InferOutputresolve to real shapes. - Read/write projections: mark fields
Type.Readonly/Type.Writeonly, derive input and output schemas withType.Write/Type.Read. Cut fields are removed from validated data, through nested objects, arrays, unions and recursive schemas. - Form-friendly validation: strings from forms, query strings and env vars are coerced by Ajv's rules, defaults are applied, issues come flat with field paths. The input is never mutated and validation never throws.
- Standard JSON Schema:
schema['~standard'].jsonSchema.input()/.output()return plain JSON Schema for draft 2020-12, draft-07 or OpenAPI 3.0. The output side of a projection forbids the keys it cut, so a tool that trims data by the JSON Schema can't let awriteOnlyfield through. The$ids you set are kept as names, except where a validator could not read the document with them. - Composition: compose by nesting, flatten objects with
Type.Composite, replace fields withType.Override, recursive types withType.Cyclic; projections survive all of it. No registry; validators are JIT-compiled and cached.
Why this package
TypeBox doesn't implement Standard Schema itself. The alternatives:
- TypeMap, by TypeBox's author, compiles a schema into a validator that carries
~standard. It is built for the 0.34 line (@sinclair/typebox), not TypeBox 1.x, and you passCompile(User)around rather thanUser. - A hand-written adapter around
typebox/compilecovers the happy path in a dozen lines. It leaves out per-field issue paths, coercion and defaults that don't touch the caller's object, typedInferInput/InferOutput, Standard JSON Schema, projections, and validation that stays fast on recursive unions. - Zod, Valibot or ArkType, if JSON Schema doesn't need to be the source of truth: see the comparison.
Documentation
- Getting started
- Projections, Composing, Recursive schemas
- Validation: the pipeline, coercion rules, issue shape,
parse - Custom formats and types, Adopting external schemas (drizzle-typebox)
- JSON Schema & OpenAPI, API reference
Contributing
Issues and pull requests are welcome: see CONTRIBUTING.md. The project uses Bun; bun run test runs everything CI does.
License
MIT © Vladimir Kraikovskii
Reviews (0)
Sign in to leave a review.
Leave a reviewNo results found