npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@artutra/ts-data-json

v1.1.0

Published

A JSON decoding library for Typescript

Downloads

15

Readme

JsonDecoder

Build Status npm version

Typescript type annotations give us compile-time guarantees, but at run-time, when data flows from the server to our clients, lots of things can go wrong.

JSON decoders validate the JSON before it comes into our program. So if the data has an unexpected structure, we learn about it immediately.

If you are new to JSON decoding, you may want to read the introductory article Decoding JSON with Typescript about why and how to use this library.

Install

npm install ts.data.json --save

Example

type User = {
  firstname: string;
  lastname: string;
};

const userDecoder = JsonDecoder.object<User>(
  {
    firstname: JsonDecoder.string,
    lastname: JsonDecoder.string
  },
  'User'
);

const jsonObjectOk = {
  firstname: 'Damien',
  lastname: 'Jurado'
};

userDecoder
  .decodePromise(jsonObjectOk)
  .then(user => {
    console.log(`User ${user.firstname} ${user.lastname} decoded successfully`);
  })
  .catch(error => {
    console.log(error);
  });

// Output: User Damien Jurado decoded successfully

const jsonObjectKo = {
  firstname: 'Erik',
  lastname: null
};

userDecoder
  .decodePromise(jsonObjectKo)
  .then(user => {
    console.log('User decoded successfully');
  })
  .catch(error => {
    console.error(error);
  });

// Output: <User> decoder failed at key "lastname" with error: null is not a valid string

API

JsonDecoder.string

string: Decoder<string>

Creates a string decoder.

JsonDecoder.string.decode('hi'); // Ok<string>({value: 'hi'})
JsonDecoder.string.decode(5); // Err({error: '5 is not a valid string'})

JsonDecoder.number

number: Decoder<number>

Creates a number decoder.

JsonDecoder.number.decode(99); // Ok<number>({value: 99})
JsonDecoder.number.decode('hola'); // Err({error: 'hola is not a valid number'})

JsonDecoder.boolean

boolean: Decoder<boolean>

Creates a boolean decoder.

JsonDecoder.boolean.decode(true); // Ok<boolean>({value: true})
JsonDecoder.boolean.decode(null); // Err({error: 'null is not a valid boolean'})

JsonDecoder.object

object<a>(decoders: DecoderObject<a>, decoderName: string, keyMap?: DecoderObjectKeyMap<a>): Decoder<a>

Creates an object decoder.

@param decoders: DecoderObject<a>

Key/value pair that has to comply with the <a> type.

Turns all optional keys to required, so you have to specify decoders even for the optional (i.e. with {name?: string}) keys.

@param decoderName: string

Type of the object we are decoding. i.e. User. It is used to generate meaningful decoding error messages.

@param keyMap?: DecoderObjectKeyMap<a>

Optional key/value pair to map JSON-land keys with Model-land keys. Useful when the JSON keys don't match with the decoded type keys.

Basic example

type User = {
  firstname: string;
  lastname: string;
};
const userDecoder = JsonDecoder.object<User>(
  {
    firstname: JsonDecoder.string,
    lastname: JsonDecoder.string
  },
  'User'
);

const jsonOk = {
  firstname: 'Damien',
  lastname: 'Jurado'
};
userDecoder.decode(jsonOk);
// Output: Ok<User>({value: {firstname: 'Damien', lastname: 'Jurado'}})

const jsonKo = {
  firstname: null,
  lastname: 'Satie'
};
userDecoder.decode(jsonKo);
// Output: Err({error: '<User> decoder failed at key "firstname" with error: null is not a valid string'})

keyMap example

const userDecoder = JsonDecoder.object<User>(
  {
    firstname: JsonDecoder.string,
    lastname: JsonDecoder.string
  },
  'User',
  {
    firstname: 'fName',
    lastname: 'lName'
  }
);

const jsonOk = {
  fName: 'Nick',
  lName: 'Drake'
};
userDecoder.decode(json);
// Output: Ok({value: {firstname: 'Nick', lastname: 'Drake'}})

const jsonKo = {
  fName: 'Nick'
};
userDecoder.decode(json);
// Output: Err({error: '<User> decoder failed at key "lastname" (mapped from the JSON key "lName") with error: undefined is not a valid string'})

JsonDecoder.objectStrict

objectStrict<a>(decoders: DecoderObject<a>, decoderName: string): Decoder<a>

Creates an object decoder that performs strict key checks. It only accepts json objects with exactly the same keys as the decoder keys.

@param decoders: DecoderObject<a>

Key/value pair that has to comply with the <a> type.

@param decoderName: string

Type of the object we are decoding. i.e. User. It is used to generate meaningful decoding error messages.

Basic example

type User = {
  firstname: string;
  lastname: string;
};
const userDecoder = JsonDecoder.objectStrict<User>(
  {
    firstname: JsonDecoder.string,
    lastname: JsonDecoder.string
  },
  'User'
);

const jsonOk = {
  firstname: 'Damien',
  lastname: 'Jurado'
};
userDecoder.decode(jsonOk);
// Output: Ok<User>({value: {firstname: 'Damien', lastname: 'Jurado'}})

const jsonKo = {
  firstname: 'Damien',
  lastname: 'Jurado',
  email: '[email protected]'
};
userDecoder.decode(jsonKo);
// Output: Err({error: 'Unknown key "email" found while processing strict <User> decoder'})

JsonDecoder.array

array<a>(decoder: Decoder<a>, decoderName: string): Decoder<Array<a>>

Creates an array decoder.

@param decoder: Decoder<a>

The decoder used to decode every Array<a> item.

@param decoderName: string

Type of the object we are decoding. i.e. User[]. It is used to generate meaningful decoding error messages.

JsonDecoder.array<number>(JsonDecoder.number, 'number[]').decode([1, 2, 3]);
// Output: Ok<number[]>({value: [1, 2, 3]})

JsonDecoder.array<number>(JsonDecoder.number, 'number[]').decode([1, '2', 3]);
// Output: Err({error: '<number[]> decoder failed at index 1 with error: "2" is not a valid number'})

JsonDecoder.dictionary

dictionary<a>(decoder: Decoder<a>, decoderName: string): Decoder<{ [name: string]: a }>

Creates a dictionary decoder.

@param decoder: Decoder<a>

The decoder used to decode every value of the key/value pairs.

@param decoderName: string

Type of the object we are decoding. i.e. User. It is used to generate meaningful decoding error messages.

JsonDecoder.dictionary(JsonDecoder.number, 'Dict<number>').decode({
  a: 1,
  b: 2
});
// Output: Ok<Dict<number>>({value: {a: 1, b: 2}})

JsonDecoder.dictionary(JsonDecoder.number, 'Dict<number>').decode({
  a: 1,
  b: 2,
  c: null
});
// Output: Err({error: '<Dict<number>> dictionary decoder failed at key "c" with error: null is not a valid number'})

JsonDecoder.oneOf

oneOf<a>(decoders: Array<Decoder<a>>, decoderName: string): Decoder<a>

The oneOf decoder tries to decode the provided JSON with any of the provided decoders. It returns Ok with the first successful decoded value or Err if all decoders fail.

@param decoders: Array<Decoder<a>>

The Array of decoders the JSON can be decoded with.

@param decoderName: string

Type of the object we are decoding. i.e. number | string. It is used to generate meaningful decoding error messages.

JsonDecoder.oneOf<string | number>(
  [JsonDecoder.string, JsonDecoder.number],
  'string | number'
).decode(1);
// Output: Ok<string | number>({value: 1})

JsonDecoder.oneOf<string | number>(
  [JsonDecoder.string, JsonDecoder.number],
  'string | number'
).decode(true);
// Output: Err({error: "<string | number> decoder failed because true can't be decoded with any of the provided oneOf decoders"})

JsonDecoder.allOf

allOf<T extends Array<Decoder<unknown>>, R = AllOfDecoderReturn<T>>(decoders: T): Decoder<R>

The allOf decoder tries to decode the provided JSON with all of the provided decoders, in order. The output of one decoder is passed as input to the next decoder. It returns Ok with the last successful decoded value or Err if any decoder fails.

The allOf decoder allows you to combine multiple decoders. It is probably most useful when combined with custom decoders you may make for your application.

@param decoders: T extends Array<Decoder<unknown>>

An array of decoders the JSON should be decoded with.

Simple examples:

JsonDecoder.allOf(
 JsonDecoder.string,
 JsonDecoder.failover(10, JsonDecoder.number)
).decode('hola'),
// Output: Ok({value: 10})

JsonDecoder.allOf(
 JsonDecoder.string,
 JsonDecoder.failover(10, JsonDecoder.number)
).decode(5),
// Output: Err({error: "5 is not a valid string})

Example using a custom hasLength() decoder (an example here):

JsonDecoder.allOf(
  JsonDecoder.array(JsonDecoder.number, 'latLang'),
  hasLength<[number, number]>(2)
).decode([-123.34324, 23.454365]);
// Output: Ok({value: [-123.34324, 23.454365]})

JsonDecoder.allOf(
  JsonDecoder.array(JsonDecoder.number, 'latLang'),
  hasLength<[number, number]>(2)
).decode([1, 2, 3]);
// Output: Err({error: "hasLength() decoder failed because the provided array is of length 3."})

JsonDecoder.lazy

lazy<a>(mkDecoder: () => Decoder<a>): Decoder<a>

Decoder for recursive data structures.

@param mkDecoder: () => Decoder<a>

A function that returns a decoder.

type Node<a> = {
  value: a;
  children?: Node<a>[];
};
const treeDecoder: JsonDecoder.Decoder<Node<string>> = JsonDecoder.object<
  Node<string>
>(
  {
    value: JsonDecoder.string,
    children: JsonDecoder.oneOf<Node<string>[]>(
      [
        JsonDecoder.lazy(() => JsonDecoder.array(treeDecoder, 'Node<a>[]')),
        JsonDecoder.isUndefined([])
      ],
      'Node<string>[] | isUndefined'
    )
  },
  'Node<string>'
);
treeDecoder.decode({
  value: 'root',
  children: [
    { value: '1' },
    { value: '2', children: [{ value: '2.1' }, { value: '2.2' }] }
  ]
});
// Output: Ok<Node<string>>({value: {value: 'root', children: [....]}})

treeDecoder.decode({
  value: 'root',
  children: null
});
// Output: Err({error: "<Node<string>> decoder failed at key 'children' with error: <Node<string>[] | isUndefined> decoder failed because null can't be decoded with any of the provided oneOf decoders"})

JsonDecoder.optional

optional<a>(decoder: Decoder<a>): Decoder<a | undefined>

The optional decoder tries to decode the provided JSON with the provided decoder if the json value is not undefined or null. This decoder is to allow for an optional value in the TypeScript definition while retaining the ability to give a detailed error message if the wrapped decoder fails.

JsonDecoder.nullable

nullable<a>(decoder: Decoder<a>): Decoder<a | null>

The nullable decoder tries to decode the provided JSON with the provided decoder, but allows for null value. It returns a detailed error message if the value is not null and the wrapped decoder fails.

interface User {
  name: string;
  email: string | null;
}

const userDecoder = JsonDecoder.object<User>(
  {
    name: JsonDecoder.string,
    email: JsonDecoder.nullable(JsonDecoder.string)
  },
  'User'
);

userDecoder.decode({ name: 'Alice', email: '[email protected]' })
// Output: Ok<User>({value: {name: 'Alice', email: '[email protected]'}})

userDecoder.decode({ name: 'Alice', email: null })
// Output: Ok<User>({value: {name: 'Alice', email: null}})

userDecoder.decode({ name: 'Alice' })
// Output: Err({error: "<User> decoder failed at key 'email' with error: undefined is not a valid string"})

@param decoder: Decoder<a>

Decoder the JSON will be decoded with if the value is not null or undefined.

type User = {
  firstname: string;
  lastname: string;
  email?: string;
};
const userDecoder = JsonDecoder.object<User>(
  {
    firstname: JsonDecoder.string,
    lastname: JsonDecoder.string,
    email: JsonDecoder.optional(JsonDecoder.string)
  },
  'User'
);

const jsonOk = {
  firstname: 'Damien',
  lastname: 'Jurado'
};

const jsonFullUser = {
  firstname: 'Damien',
  lastname: 'Jurado',
  email: '[email protected]'
};

const jsonKo = {
  firstname: null,
  lastname: 'Satie'
};

JsonDecoder.optional(userDecoder).decode(null);
// Output: Ok<User | undefined>({value: undefined})

JsonDecoder.optional(userDecoder).decode(undefined);
// Output: Ok<User | undefined>({value: undefined})

JsonDecoder.optional(userDecoder).decode(jsonOk);
// Output: Ok<User | undefined>({value: {firstname: 'Damien', lastname: 'Jurado', email: undefined}})

JsonDecoder.optional(userDecoder).decode(jsonFullUser);
// Output: Ok<User | undefined>({value: {firstname: 'Damien', lastname: 'Jurado', email: '[email protected]'}})

JsonDecoder.optional(userDecoder).decode(jsonKo);
// Output: Err({error: '<User> decoder failed at key "firstname" with error: null is not a valid string'})

JsonDecoder.failover

failover<a>(defaultValue: a, decoder: Decoder<a>): Decoder<a>

Creates a decoder that returns a default value on failure.

@param defaultValue: a

The Ok default value when the decoder fails.

@param decoder: Decoder<a>

Decoder the JSON will be decoded with.

JsonDecoder.failover('default value', JsonDecoder.string).decode(
  'This is fine'
);
// Ok<string>({value: 'This is fine'})

JsonDecoder.failover('default value', JsonDecoder.string).decode(null);
// Ok<string>({value: 'default value'})

JsonDecoder.succeed

succeed: Decoder<any>

Creates a decoder that always succeeds.

JsonDecoder.succeed.decode(null); // Ok<any>({value: null})

JsonDecoder.fail

fail<a>(error: string): Decoder<a>

Creates a decoder that always fails.

@param error: string

Error message that will be returned with the Err instance.

JsonDecoder.fail('Something wrong happened').decode('This is fine');
// Err({error: 'Something wrong happened'})

JsonDecoder.isNull

isNull<a>(defaultValue: a): Decoder<a>

Succeeds when JSON is strictly (===) null and returns a defaultValue.

@param defaultValue: a

Returned default value when JSON is null.

JsonDecoder.isNull('default value').decode(null);
// Ok({value: 'default value'})

JsonDecoder.isNull('default value').decode(999);
// Err({error: '999 is not null'})

JsonDecoder.isUndefined

isUndefined<a>(defaultValue: a): Decoder<a>

Succeeds when JSON is strictly (===) undefined and returns a defaultValue.

@param defaultValue: a

Returned default value when JSON is undefined.

JsonDecoder.isUndefined('default value').decode(undefined);
// Ok({value: 'default value'})

JsonDecoder.isUndefined('default value').decode(999);
// Err({error: '999 is not undefined'})

JsonDecoder.isExactly

isExactly<a>(value: a): Decoder<a>

Succeeds when JSON is strictly (===) value: a and returns value: a.

@param value: a

Value returned when the JSON is strictly equal to it.

JsonDecoder.isExactly(true).decode(true);
// Ok({value: true})

JsonDecoder.isExactly(999).decode(true);
// Err({error: 'true is not 999'})

JsonDecoder.constant

constant<a>(value: a): Decoder<a>

A Decoder that always succeeds, returning value.

@param value: a

Value always returned.

JsonDecoder.constant(true).decode(false);
// Ok({value: true})

Related libraries

  • https://github.com/gcanti/io-ts
  • https://github.com/kofno/jsonous
  • https://github.com/jquense/yup
  • https://gitlab.com/john.carroll.p/ts-decoders