JSPM

  • Created
  • Published
  • Downloads 1120
  • Score
    100M100P100Q24083F
  • License MIT

Unified interface for TypeScript schema validations

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

License NPM Downloads

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 GitHub Stars z.string()
yup GitHub Stars string()
joi GitHub Stars Joi.string()
superstruct GitHub Stars string()
io-ts GitHub Stars t.string
ow GitHub Stars ow.string
typebox GitHub Stars Type.String()
typia GitHub Stars typia.createAssert<string>()
runtypes GitHub Stars String
arktype GitHub Stars 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 exception

Inspired by tRPC's input & output validators.