DEV Community

Alex Spinov
Alex Spinov

Posted on

TypeBox Has a Free API — JSON Schema Validation at TypeScript Speed

TypeBox creates JSON Schema types that are also TypeScript types. One definition, two systems — compile-time safety and runtime validation from the same code.

What is TypeBox?

TypeBox generates standard JSON Schema objects that double as TypeScript types. It works with any JSON Schema validator (Ajv, etc.) while giving you full TypeScript inference.

Quick Start

npm install @sinclair/typebox
Enter fullscreen mode Exit fullscreen mode
import { Type, Static } from '@sinclair/typebox';
import { Value } from '@sinclair/typebox/value';

const User = Type.Object({
  name: Type.String(),
  email: Type.String({ format: 'email' }),
  age: Type.Number({ minimum: 0 }),
});

type User = Static<typeof User>;
// { name: string; email: string; age: number }

// Validate
const isValid = Value.Check(User, { name: 'Alice', email: 'a@b.com', age: 30 });
// true

// Get errors
const errors = [...Value.Errors(User, { name: 123 })];
Enter fullscreen mode Exit fullscreen mode

Common Types

// Primitives
Type.String();
Type.Number();
Type.Boolean();
Type.Null();

// Arrays and Tuples
Type.Array(Type.String());
Type.Tuple([Type.String(), Type.Number()]);

// Unions and Intersections
Type.Union([Type.String(), Type.Number()]);
Type.Intersect([UserBase, UserProfile]);

// Optional and Readonly
Type.Object({
  required: Type.String(),
  optional: Type.Optional(Type.String()),
  readonly: Type.Readonly(Type.String()),
});

// Enums
Type.Union([
  Type.Literal('active'),
  Type.Literal('inactive'),
]);
Enter fullscreen mode Exit fullscreen mode

Using with Ajv (Maximum Performance)

import Ajv from 'ajv';
import { Type } from '@sinclair/typebox';

const ajv = new Ajv();
const schema = Type.Object({
  id: Type.String({ format: 'uuid' }),
  items: Type.Array(Type.Object({
    name: Type.String(),
    price: Type.Number({ minimum: 0 }),
  })),
});

const validate = ajv.compile(schema);

if (validate(data)) {
  // data is typed as Static<typeof schema>
}
Enter fullscreen mode Exit fullscreen mode

Value Utilities

import { Value } from '@sinclair/typebox/value';

// Create default values
const defaultUser = Value.Create(User);
// { name: '', email: '', age: 0 }

// Cast (coerce) data
const casted = Value.Cast(User, { name: 123, extra: true });
// { name: '123', email: '', age: 0 }

// Clone
const cloned = Value.Clone(data);

// Diff and Patch
const diff = Value.Diff(oldUser, newUser);
const patched = Value.Patch(oldUser, diff);
Enter fullscreen mode Exit fullscreen mode

Why TypeBox?

  • Standards-based — generates real JSON Schema (use with any validator)
  • Zero dependencies — tiny bundle size
  • Fastest — compiled validators outperform Zod by 1000x+
  • Elysia/Fastify native — built-in TypeBox support

Need structured data from the web? Check out my Apify scrapers for ready-to-use data extraction tools, or email spinov001@gmail.com for custom solutions.

TypeBox or Zod — which do you prefer? Let me know in the comments!

Top comments (0)