typebox-standard-schema

mcp
Security Audit
Pass
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.

SUMMARY

Standard Schema for TypeBox: use TypeBox schemas in tRPC, Hono, Elysia and TanStack Form, with read/write projections

README.md

typebox-standard-schema

npm version
CI
license: MIT

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:

Features

  • Standard Schema out of the box: no adapter, no compile step; StandardSchemaV1.InferInput / InferOutput resolve to real shapes.
  • Read/write projections: mark fields Type.Readonly / Type.Writeonly, derive input and output schemas with Type.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 a writeOnly field 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 with Type.Override, recursive types with Type.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 pass Compile(User) around rather than User.
  • A hand-written adapter around typebox/compile covers 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, typed InferInput / 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

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)

No results found