New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@decs/typeschema

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@decs/typeschema

Unified interface for TypeScript schema validations

  • 0.1.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

🛵 TypeSchema

Unified interface for TypeScript schema validations

License NPM Downloads


Many libraries rely on some sort of type validation. Their maintainers have the choice of either to:

  1. Implement their own validation logic: which leads to more code to maintain, and we already have many good solutions out there (e.g. zod, arktype, typia)
  2. Couple their code with a specific validation library: which limits adoption by developers who use another
  3. Support multiple validation libraries: which is a burden to keep up-to-date (tRPC picked this one)

There's no best validation library because there's always a tradeoff. Each developer chooses the library that makes the most sense to them. TypeSchema solves this problem by easily providing option 3: support multiple validation libraries out-of-the-box.

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:

npmnpm install @decs/typeschema
Yarnyarn add @decs/typeschema
pnpmpnpm add @decs/typeschema

Usage

import type {Infer, Schema} from '@decs/typeschema';
import {assert} from '@decs/typeschema';

// Use your favorite validation library, e.g. `zod`, `arktype`, `typia`
const schema: Schema<string> = z.string();
const schema: Schema<string> = type('string');
const schema: Schema<string> = typia.createAssert<string>();

// Extracts the schema type
type Type = Infer<typeof schema>; // `string`

// Returns the validated data or throws an exception
await assert(schema, '123'); // '123'
await assert(schema, 123); // `Error`

API

Types
  • Schema<T>
    Generic interface for schemas
  • Infer<T as Schema<unknown>>
    Extracts the equivalent TypeScript type of a schema
Functions
  • assert<T>(schema: Schema<T>, data: unknown): Promise<T>
    Returns the validated data or throws an exception

Coverage

ProjectPopularityExample schemaSupport
zodGitHub Starsz.string()
yupGitHub Starsstring()
joiGitHub StarsJoi.string()
superstructGitHub Starsstring()
io-tsGitHub Starst.string
owGitHub Starsow.string
typeboxGitHub StarsType.String()
typiaGitHub Starstypia.createAssert<string>()
runtypesGitHub StarsString
arktypeGitHub Starstype('string')

Custom validations are also supported:

export function assertString(data: unknown): string {
  if (typeof data !== 'string') {
    throw new Error('Expected a string, got: ' + data);
  }
  return data;
}

await assert(assertString, '123'); // Returns '123'
await assert(assertString, 123); // Throws an exception

Acknowledgements

Keywords

FAQs

Package last updated on 06 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc