Socket
Socket
Sign inDemoInstall

decoders

Package Overview
Dependencies
0
Maintainers
1
Versions
134
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    decoders

Elegant and battle-tested validation library for type-safe input data for TypeScript


Version published
Maintainers
1
Install size
97.2 kB
Created

Changelog

Source

[2.3.0] - 2024-01-09

New features:

  • All enum types are now supported (docs)
  • Record decoder now supports both record(values) and record(keys, values) forms (docs)
  • Add datelike decoder (docs)
  • Add support for bigint (docs)
  • Add built-in support for common string validations
  • Better support for symbols in constant() and oneOf()

New decoders:

Renamed decoders:

Some decoders have been renamed because their names were based on Flowisms. Names have been updated to better reflect TypeScript terminology:

  • dict()record()
  • maybe()nullish()
  • set()setFromArray() (to make room for a better set() decoder in a future version)

Deprecated decoders:

The following decoders are deprecated because they were not commonly used, and a bit too specific to be in the standard library. They are also scheduled for removal in a future decoders version.

  • dict() (prefer record())
  • hardcoded() (prefer always())
  • maybe() (prefer nullish())
  • mixed (prefer unknown)
  • numericBoolean()

Other changes:

  • Fix: positiveNumber and positiveInteger no longer accept -0 as valid inputs
  • Fix: either return type would sometimes get inferred incorrectly if members partially overlapped (see #941)
  • Reorganized internal module structure
  • Simplified some of the more complicated internal types

Readme

Source

Decoders logo

npm Build Status Minified Size

Elegant and battle-tested validation library for type-safe input data for TypeScript.

Introduction

Data entering your application from the outside world should not be trusted without validation and often is of the any type, effectively disabling your type checker around input values. It's an industry good practice to validate your expectations right at your program's boundaries. This has two benefits: (1) your inputs are getting validated, and (2) you can now statically know for sure the shape of the incoming data. Decoders help solve both of these problems at once.

Basic example

import { array, iso8601, number, object, optional, string } from 'decoders';

// Incoming data at runtime
const externalData = {
  id: 123,
  name: 'Alison Roberts',
  createdAt: '1994-01-11T12:26:37.024Z',
  tags: ['foo', 'bar'],
};

// Write the decoder (= what you expect the data to look like)
const userDecoder = object({
  id: number,
  name: string,
  createdAt: optional(iso8601),
  tags: array(string),
});

// Call .verify() on the incoming data
const user = userDecoder.verify(externalData);
//    ^^^^
//    TypeScript automatically infers this type as:
//    {
//      id: number;
//      name: string;
//      createdAt?: Date;
//      tags: string[];
//    }

Installation

npm install decoders

Requirements

You must set strict: true in your tsconfig.json in order for type inference to work correctly!

// tsconfig.json
{
  "compilerOptions": {
    "strict": true
  }
}

Documentation

Documentation can be found on https://decoders.cc.

Building your own decoders

There is a dedicated page in the docs that explains how to build your own decoders — it’s fun!

Keywords

FAQs

Last updated on 09 Jan 2024

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc