You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

decode-ts

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

decode-ts

TypeScript compatible value decoding.


Version published
Weekly downloads
488
decreased by-25.72%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

decode-ts

TypeScript compatible value decoding.

  • io-ts is used to perform IO validation for type safety.
  • fp-ts is used for its Either type.

This is mostly a wrapper around the terrific io-ts to add a call to JSON.parse. You could achieve something similar with an io-ts type, however there is no way to propoagate the JSON parsing errors, hence the need for the wrapper.

Installation

yarn add decode-ts

Example

import * as t from 'io-ts';

import { jsonDecodeString, reportJsonDecodeError } from './index';

const Person = t.interface({
    id: t.string,
    age: t.number,
});

const log = (val: any) => console.log(JSON.stringify(val));

log(jsonDecodeString(Person)('foo')); // Left ParsingError
log(jsonDecodeString(Person)('{ "id": 1 }')); // Left ValidationErrors
log(jsonDecodeString(Person)('{ "id": "foo", "age": 5 }')); // Right { id: 'foo', age: 5 }

// Use the reporter for friendly error messages:

log(reportJsonDecodeError(jsonDecodeString(Person)('foo')));
// ["Unexpected token o in JSON at position 1"]
log(reportJsonDecodeError(jsonDecodeString(Person)('{ "id": 1 }')));
// ["Expecting string at id but instead got: 1.","Expecting number at age but instead got: undefined."]

Development

yarn
yarn compile
yarn lint

FAQs

Package last updated on 04 Feb 2020

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc