Skip to content

decs/typeschema

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation


TypeSchema

TypeSchema

Universal adapter for schema validation
✨ https://typeschema.com ✨


License Bundle size npm downloads GitHub stars

Quickstart Β Β β€’Β Β  Coverage Β Β β€’Β Β  API Β Β β€’Β Β  GitHub Β Β β€’Β Β  npm


When fetching data from an external source, it's important to verify its integrity. This happens when processing user inputs, calling third-party APIs, loading configuration files, and so on. And the thing is: Typescript doesn't come with runtime validation. Any type assertions are removed at compile-time.

As a result, developers turn to third-party validation libraries. But that landscape is fragmented, lacking a single best option. Each offers different trade-offs on developer experience, bundle size, and community support.

TypeSchema enables writing code that works with any validation library out-of-the-box. It provides a universal adapter for interacting with any validation schema, decoupling from implementation specifics and increasing compatibility.

import {validate} from '@typeschema/main';

import {z} from 'zod';
import {string} from 'valibot';

const zodSchema = z.string();
await validate(zodSchema, '123');
//    ^? {success: true, data: '123'}

const valibotSchema = string();
await validate(valibotSchema, 123);
//    ^? {success: false, issues: [...]}

Quickstart

We value flexibility, which is why there are multiple ways of using TypeSchema:

  1. Using an adapter directly (e.g. @typeschema/valibot): Best pick for end developers, when the validation library is known ahead of time. This is particularly useful for supporting more validation libraries on tRPC.
  2. Handpicking adapters with @typeschema/main: Recommended for library maintainers. Any validation library can be used, but adapters have to be explicitly installed. This allows end developers to trade-off between coverage and bundle size.
  3. Batteries included with @typeschema/all: Easiest to use. All adapters are automatically installed, including future ones. This is a drop-in replacement for the deprecated @decs/typeschema.

Coverage

Project Popularity Infer InferIn Validation Serialization Adapter Downloads
zod GitHub stars βœ… βœ… βœ… βœ… @typeschema/zod npm downloads
yup GitHub stars βœ… βœ… βœ… βœ… @typeschema/yup npm downloads
joi GitHub stars 🧐 🧐 βœ… βœ… @typeschema/joi npm downloads
ajv GitHub stars βœ… βœ… βœ… βœ… @typeschema/json npm downloads
class-validator GitHub stars βœ… βœ… βœ… 🧐 @typeschema/class-validator npm downloads
effect GitHub stars βœ… βœ… βœ… βœ… @typeschema/effect npm downloads
superstruct GitHub stars βœ… 🧐 βœ… 🧐 @typeschema/superstruct npm downloads
io-ts GitHub stars βœ… βœ… βœ… 🧐 @typeschema/io-ts npm downloads
valibot GitHub stars βœ… βœ… βœ… βœ… @typeschema/valibot npm downloads
typebox GitHub stars βœ… βœ… βœ… βœ… @typeschema/typebox npm downloads
typia GitHub stars βœ… βœ… βœ… 🧐 @typeschema/function npm downloads
arktype GitHub stars βœ… βœ… βœ… 🧐 @typeschema/arktype npm downloads
ow GitHub stars βœ… βœ… βœ… 🧐 @typeschema/ow npm downloads
deepkit GitHub stars 🧐 🧐 βœ… 🧐 @typeschema/deepkit npm downloads
runtypes GitHub stars βœ… βœ… βœ… 🧐 @typeschema/runtypes npm downloads
fastest-validator GitHub stars 🧐 🧐 βœ… 🧐 @typeschema/fastest-validator npm downloads
vine GitHub stars βœ… βœ… βœ… 🧐 @typeschema/vine npm downloads
suretype GitHub stars βœ… βœ… βœ… βœ… @typeschema/suretype npm downloads
valita GitHub stars βœ… βœ… βœ… 🧐 @typeschema/valita npm downloads

Note

Don't see your favorite validation library? We welcome PRs! Otherwise, please file an issue to help us prioritize. πŸ™Œ

API

Inference

  • Infer<TSchema>: Extracts the output type of a schema
  • InferIn<TSchema>: Extracts the input type of a schema

Validation

  • wrap(schema): Returns the wrapped schema with access to its operations
  • validate(schema, data): Returns the validated data or a list of validation issues
  • assert(schema, data): Returns the validated data or throws an AggregateError

Serialization

  • toJSONSchema(schema): Converts the schema into the equivalent JSON schema

Acknowledgements