Object with arbitrary keys and a single value type. Used by dict.
Represents the property of an object to decode. See object for more details.
Intelligently decode a value based on the result of another decoder. Useful decoding on object whose type or structure is not immediately known. The first decoder can be used to look at part of the object, then the second one can can use the result of that decoder to decide on how to actually decode the object.
First decoder to run against the value.
Callback that receives the previous result and returns a decoder.
The decoder returned by from the callback function.
Reaches into a nested data structure and decodes the value there. Useful if you only care about a single value in a nested data structure, or if you want to skip past a top level data key. Can perform lookups on nested arrays and objects.
Path of key lookups into a nested object.
Decoder to use on the nested value.
A decoder that decodes the value of the nested object.
A Decoder that decodes a boolean.
Decode a value and make sure the result equals another value. Useful for
checking for null.
const decoder = object(
['shouldBeNull', equal(null)],
(shouldBeNull) => ({shouldBeNull})
)
decoder.decodeJSON('{"shouldBeNull": null}')
Value that the input must equal (===).
A Decoder that decodes a value that equals the given value.
A decoder that always fails with the given error message.
Message to throw when this decoder is used.
Never since it always throws an error.
Decode a value with the decoder returned by the given function. This is useful if you need to decode a recursive data structure. TypeScript does not allow the direct use of variables within their definition:
interface Comment {
msg: string
replies: Comment[]
}
const decoder: Decoder<Comment> = object(
['msg', string()],
['replies', array(decoder)],
(msg, replies) => ({msg, replies})
)
// Block-scoped variable 'decoder' used before its declaration.
// Variable 'decoder' is used before being assigned.
However, a variable can be used within its definition if it is not immediately evaluated, or in other words, wrapped in an anonymous function:
const decoder: Decoder<Comment> = object(
['msg', string()],
['replies', array(lazy(() => decoder))],
(msg, replies) => ({msg, replies})
)
A function that will return the decoder.
A decoder that lazily calls thunk to get the decoder when needed.
Maps a function over the value returned by a decoder. Useful for creating fancier objects from builtin types.
import Immutable from 'immutable'
const decoder = map(
Immutable.List,
array(number())
)
A decoder that will apply the function after decoding the input.
A Decoder that decodes a number.
Decode an object with the given fields and types.
One or more EntryDecoders describing the object's fields.
Constructor that uses the results from the given EntryDecoders.
A Decoder that will decode an object of the given type.
Try multiple decoders on a value. Useful when paired with union types, or as
a way to provide a default value if something goes wrong. No decoders after
the first successful one will be tried. In that way oneOf can be thought
of a short circuit OR operator for decoders.
The first/rest split is to make sure at least one decoder is specified.
First decoder to try.
Fallback decoders to try in order if the first fails.
A decoder or throws an Error of no decoders succeeded.
A Decoder that decodes a string.
A decoder that always succeeds with the given value.
Value that always gets returned when this decoder is used.
A decoder that always returns the given value.
Decodes a tuple from an array. If the length of the array is not known at compile time, you're probably looking for array.
One or more decoders for each element of the array.
A decoder return a tuple containing each decoded element.
Decode a union type. Given multiple decoders whose result type is a member of the union, try each one in order until one succeeds. Works similarly to oneOf, except that instead of the result being of single type, the result is the union of all given decoder types.
A decoder for a member of the union.
A decoder for another member of the union.
A decoder or throws an Error of no decoders succeeded.
Generated using TypeDoc
This module can be used to safely decode JSON into a typed value. All of the decoders in this module are pure and reusable.