A decoder that can validate and transform JSON data into strongly typed TypeScript values.

Type Parameters

  • T

    The type that this decoder will produce when successful

Implements

  • StandardSchemaV1<unknown, T>

Constructor

Entry Point

Transformation

Constructor

  • Creates a new decoder that can validate and transform JSON data into strongly typed TypeScript values.

    Type Parameters

    • T

    Parameters

    • decodeFn: (json: any) => Result<T>

      A function that takes a JSON object and returns a Result

    Returns Decoder<T>

    const myStringDecoder: JsonDecoder.Decoder<string> = new JsonDecoder.Decoder(
    (json: unknown) => {
    if (typeof json === 'string') {
    return ok(json);
    } else {
    return err('Expected a string');
    }
    }
    );

Entry Point

"~standard": Props<unknown, T> = ...

The Standard Schema interface for this decoder.

  • Decodes a JSON object of type and returns a Result

    Parameters

    • json: any

      The JSON object to decode

    Returns Result<T>

    A Result containing either the decoded value or an error message

    JsonDecoder.string.decode('hi'); // Ok<string>({value: 'hi'})
    JsonDecoder.string.decode(5); // Err({error: '5 is not a valid string'})
  • Decodes a JSON object of type and returns a Promise

    Parameters

    • json: any

      The JSON object to decode

    Returns Promise<T>

    A Promise that resolves with the decoded value or rejects with an error message

    JsonDecoder.string.decodeToPromise('hola').then(res => console.log(res)); // 'hola'
    JsonDecoder.string.decodeToPromise(2).catch(err => console.log(err)); // '2 is not a valid string'
  • Decodes a JSON object of type and calls onOk() on success or onErr() on failure, both return

    Type Parameters

    • O

    Parameters

    • onOk: (result: T) => O

      function called when the decoder succeeds

    • onErr: (error: string) => O

      function called when the decoder fails

    • json: any

      The JSON object to decode

    Returns O

    The result of either onOk or onErr

    JsonDecoder.string.fold(
    (value: string) => parseInt(value, 10),
    (error: string) => 0,
    '000000000001'
    ); // 1

Transformation

  • Chain decoders that might fail

    Type Parameters

    • O

    Parameters

    • fn: (value: T) => Decoder<O>

      Function that returns a new decoder

    Returns Decoder<O>

    A new decoder that chains the current decoder with the result of fn

    const adultDecoder = JsonDecoder.number.chain(age =>
    age >= 18
    ? JsonDecoder.succeed
    : JsonDecoder.fail(`Age ${age} is less than 18`)
    );
    adultDecoder.decode(18); // Ok<number>({value: 18})
    adultDecoder.decode(17); // Err({error: 'Age 17 is less than 18'})
  • If the decoder has succeeded, transforms the decoded value into something else

    Type Parameters

    • O

    Parameters

    • fn: (value: T) => O

      The transformation function

    Returns Decoder<O>

    A new decoder that applies the transformation

    // Decode a string, then transform it into a Date
    const dateDecoder = JsonDecoder.string.map(stringDate => new Date(stringDate));
    // Ok scenario
    dateDecoder.decode('2018-12-21T18:22:25.490Z'); // Ok<Date>({value: Date(......)})
    // Err scenario
    dateDecoder.decode(false); // Err({error: 'false is not a valid string'})