decoders

Arrays

Array and tuple decoders for validating collections of values.

array

array(decoder: Decoder<T>): Decoder<T[]> (source)

Accepts arrays of whatever the given decoder accepts.

const decoder = array(string);

// 👍
decoder.verify(['hello', 'world']) === ['hello', 'world'];
decoder.verify([]) === [];

// 👎
decoder.verify(['hello', 1.2]);  // throws

nonEmptyArray

nonEmptyArray(decoder: Decoder<T>): Decoder<[T, ...T[]]> (source)

Like array(), but will reject arrays with 0 elements.

const decoder = nonEmptyArray(string);

// 👍
decoder.verify(['hello', 'world']) === ['hello', 'world'];

// 👎
decoder.verify(['hello', 1.2]);  // throws
decoder.verify([]);              // throws

poja

poja: Decoder<unknown[]> (source)

Accepts any array, but doesn't validate its items further.

"poja" means "plain old JavaScript array", a play on pojo.

// 👍
poja.verify([1, 'hi', true]) === [1, 'hi', true];
poja.verify(['hello', 'world']) === ['hello', 'world'];
poja.verify([]) === [];

// 👎
poja.verify({});    // throws
poja.verify('hi');  // throws

tuple

tuple(Decoder<A>, Decoder<B>, ...): Decoder<[A, B, ...]> (source)

Accepts a tuple (an array with exactly n items) of values accepted by the n given decoders.

const decoder = tuple(string, number);

// 👍
decoder.verify(['hello', 1.2]) === ['hello', 1.2];

// 👎
decoder.verify([]);                  // throws, too few items
decoder.verify(['hello', 'world']);  // throws, not the right types
decoder.verify(['a', 1, 'c']);       // throws, too many items

On this page