Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

common-decoders

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

common-decoders

A simple, functional, typescript schema library for generating types and runtime checks.

  • 0.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Common Decoders

test-build Coverage Status

A minimal typescript schema library for runtime validations.

Write your type definitions once and get both types and runtime validation checks. Decoders are light-weight, composable, and follow functional programming patterns.

Installation

# NPM
npm install --save common-decoders

# Yarn
yarn add common-decoders

Requirements

  • Typescript >= 4.7
  • Node >= 16

Basic Usage

Example

import * as D from 'common-decoders';

const IdentifableDecoder = D.object({
  id: D.number,
})

const DogDecoder = D.object({
  type: D.literal('dog'),
  name: D.string,
})

const CatDecoder = D.object({
  type: D.literal('cat'),
})

const AnimalDecoder = D.union([
  IdentifableDecoder,
  D.intersection([DogDecoder, CatDecoder]),
])

type Animal = D.Infer<typeof AnimalDecoder>;

API

Full documentation is available here.

Or can be built locally by running: yarn docs;

ESM or CJS Imports

TL;DR; prefer ESM

This library supports both commonjs and modern es modules. However, it is more ergonomic for ESM usage.

Importing with tsconfig compilation option for moduleResolution set to nodenext | node16:

import * as D from 'common-decoders';
import { isOk, isFailure, map, pipe } from 'common-decoders/utils';

Or with moduleResolution set to node:

import * as D from 'common-decoders';
import { isOk, isFailure, map, pipe } from 'common-decoders/dist/cjs/utils';

Prior art:

This is not a new problem nor a particularly unique solution. The aim of this library is to build on previous work, while being super small, simple to use, and functional in style.

For more mature and comprehensive approaches - have a look at:

FAQs

Package last updated on 31 Aug 2022

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