🌙 TypeSchema
Unified interface for TypeScript schema validations
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 = [...];
type Type = Infer<typeof schema>;
await assert(schema, value);
Coverage
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');
await assert(assertString, 123);