Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/decs/typeschema

🛵 Universal adapter for TypeScript schema validation.
https://github.com/decs/typeschema

adapter assert inference schema type typescript validation

Last synced: about 2 months ago
JSON representation

🛵 Universal adapter for TypeScript schema validation.

Awesome Lists containing this project

README

        




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](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertions).
>
> 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](#coverage) out-of-the-box. It provides a universal adapter for interacting with any validation schema, decoupling from implementation specifics and increasing compatibility.

```ts
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`](https://github.com/decs/typeschema/tree/main/packages/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](https://trpc.io/).
2. **Handpicking adapters** with [`@typeschema/main`](https://github.com/decs/typeschema/tree/main/packages/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`](https://github.com/decs/typeschema/tree/main/packages/all): Easiest to use. All adapters are automatically installed, including future ones. This is a drop-in replacement for the deprecated [`@decs/typeschema`](https://www.npmjs.com/package/@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


superstruct
GitHub stars

🧐

🧐
@typeschema/superstruct
npm downloads


io-ts
GitHub stars



🧐
@typeschema/io-ts
npm downloads


effect
GitHub stars




@typeschema/effect
npm downloads


valibot
GitHub stars




@typeschema/valibot
npm downloads


typebox
GitHub stars




@typeschema/typebox
npm downloads


typia
GitHub stars



🧐
@typeschema/function
npm downloads


ow
GitHub stars



🧐
@typeschema/ow
npm downloads


arktype
GitHub stars



🧐
@typeschema/arktype
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](https://github.com/decs/typeschema/pulls)!
> Otherwise, please [file an issue](https://github.com/decs/typeschema/issues) to help us prioritize. 🙌

## API

### Inference
- `Infer`: Extracts the output type of a schema
- `InferIn`: 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

- Inspired by [tRPC](https://trpc.io/)'s [input & output validators](https://trpc.io/docs/server/validators)
- Adapter architecture inspired by [@ecyrbe](https://github.com/ecyrbe)'s [suggestions](https://github.com/decs/typeschema/issues/1)
- API definition inspired by [@colinhacks](https://github.com/colinhacks)'s [proposal](https://twitter.com/colinhacks/status/1634284724796661761)
- Logo designed by [flaticon](https://www.flaticon.com/)