A type representing the result of a decoding process
Decodes a boolean
Decodes the value null
null_.decode(null) // => ✅ null
null_.decode(undefined) // => 🟥 "Expected null, got undefined instead"
Decodes a number
number.decode(42) // => ✅ 42
number.decode("42") // => 🟥 "Expected a number, got \"42\" instead"
Decodes a string
Decodes the value undefined
undefined_.decode(undefined) // => ✅ undefined
undefined_decode(null) // => 🟥 "Expected undefined, got null instead"
Leave the value as it is without making any assumptions about its type. Useful for dealing with types later
unknown.decode([1, 2, 3]) // => ✅ [1, 2, 3]
Decodes an exact value. Useful in combination with oneOf
for creating enums.
const dec = hardcoded("TAG") // => Decoder<"TAG">
dec.decode("TAG") // => ✅ "TAG"
dec.decode("not tag") // => 🟥 "Expected "TAG", got \"not tag\" instead"
A decoder that never succeeds.
never("invalid value").decode(42) // => 🟥 "invalid value"
A decoder that always returns the same value, ignoring the given input. This decoder always suceeds.
succeed(42).decode("ignored value") // => ✅ 42
A type representing the fields (either optional or mandatory) required by the object
decoder
Given a Decoder
of type T
, decodes a object as a string => T
map.
Useful when keys are not statically known
dict(number).decode({ x: 0, y: 1 }) // => ✅ { x: 0, y: 1 }
dict(number).decode({ x: 0, y: "str" }) // => 🟥
dict(number) // Decoder<Partial<{ [key: string]: number | undefined; }>>
Wraps a decode into a thunk. Useful for creating decoders for recursive data structures
type Tree = {
label: string;
children: Tree[];
};
const treeDecoder: Decoder<Tree> = object({
label: string.required,
children: lazy(() => array(treeDecoder)).required,
});
Decodes a object with known fields.
See Decoder.required
, Decoder.optional
, and Decoder.default
field types
const person = object({
name: string.required,
id: number.required,
})
person.decode({ name: "john doe", id: 11234 }) // => ✅
A type representing the reason of a decoding failure
Convert to a json-like string
Convert to a xml-like string
Generated using TypeDoc
Utility type useful for extracting the return type of a Decoder