interface Codec<T> {
    decode: (input: unknown) => functional.Either<string, T>;
    encode: <U = unknown>(input: T) => U;
    schema: () => JSONSchema6;
    unsafeDecode: (input: unknown) => T;
}

Type Parameters

  • T

Properties

decode: (input: unknown) => functional.Either<string, T>

Takes a JSON value and runs the decode function the codec was constructed with. All of purify's built-in codecs return a descriptive error message in case the decode fails

encode: <U = unknown>(input: T) => U

Takes a runtime value and turns it into a JSON value using the encode function the codec was constructed with. Most of purify's built-in codecs have no custom encode method and they just return the same value, but you could add custom serialization logic for your custom codecs.

schema: () => JSONSchema6
unsafeDecode: (input: unknown) => T

The same as the decode method, but throws an exception on failure. Please only use as an escape hatch