Package Exports
- @decs/typeschema
- @decs/typeschema/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@decs/typeschema) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
🛵 TypeSchema
Unified interface for TypeScript schema validations
Features
- 🚀 Decouple your code from validation libraries
- 🍃 Less than 3 kB, zero dependencies
- ✨ Easy-to-use, minimal API
Setup
Install TypeSchema with your package manager of choice:
| npm | npm install @decs/typeschema |
|---|---|
| Yarn | yarn add @decs/typeschema |
| pnpm | pnpm add @decs/typeschema |
Usage
import type {Infer} from '@decs/typeschema';
import {assert} from '@decs/typeschema';
const schema = ... // Use your favorite validation library
// Extracts the schema type
type Type = Infer<typeof schema>;
// Returns the validated value or throws an exception
await assert(schema, value);Coverage
| Project | Popularity | Example schema | Support |
|---|---|---|---|
| zod | z.string() |
✅ | |
| yup | string() |
✅ | |
| joi | Joi.string() |
✅ | |
| superstruct | string() |
✅ | |
| io-ts | t.string |
❌ | |
| ow | ow.string |
❌ | |
| typebox | Type.String() |
❌ | |
| typia | typia.createAssert<string>() |
✅ | |
| runtypes | String |
✅ | |
| arktype | type('string') |
✅ |
Custom validations are also supported:
export function assertString(value: unknown): string {
if (typeof value !== 'string') {
throw new Error('Expected a string, got: ' + value);
}
return value;
}
await assert(assertString, '123'); // Returns '123'
await assert(assertString, 123); // Throws an exceptionInspired by tRPC's input & output validators.