Parse
sourceFunctions for parsing from JSON values into SDL-supported types.
Each parsing function accepts a value and a ParserTracer, which is used to record parsing/validation errors. Functions return either the parsed value, or undefined; if undefined is returned (and unexpected) the caller should use the getMessage method to extract the error.
If undefined is a valid value, the caller should check for this before invoking the parsing function.
For more information on SDL datatypes and their serialisation, refer to: https://gitlab.cartax.io/platform/sdl/-/blob/master/docs/datatypes.md
Constructors
new Parse(): Parse
Methods
Static methods
any<T = any>(x: T): Tsource
The identity function.
array<T>(elementParser: ParseFn): ParseFnsource
Returns a parser that parses an array, applying the given elementParser to each element.
The array will be rejected if the elementParser returns undefined for any element of the array.
bool(x: any, t: ParserTracer): undefined | booleansource
Parses a boolean value.
date(x: any, t: ParserTracer): undefined | PlainDatesource
Parses a string in ISO8601 date format (YYYY-MM-DD) into a Temporal.PlainDate.
decimal<N extends number>(numDecimals: N): ParseFnsource
Returns a parser that parsers a string to a Decimal, with an optional fixed number of decimals.
enum<Domain extends EnumDomain>(name: string, domain: Domain): ParseFnsource
Returns a parser that parses a string to a value of the given enumeration domain.
fromArrayUsingQueryMetadata<T>(queryMetadata: QueryReflectionMetadata): Functionsource
Returns a parser that parses an array of values into the desired object type, using the given reflection metadata.
fromObjectUsingFastPathWithConstructor<T>(metadata: ReflectionMetadata, constructor: Function): ParseFnsource
Returns a parser that instantiates its object by invoking a constructor function whose arguments correspond to the fields of the structure.
fromObjectUsingMetadata<T>(metadata: ReflectionMetadata): ParseFnsource
Returns a parser that parses an object using the given reflection metadata.
fromObjectUsingSlowPathWithObjectCreate<T>(metadata: ReflectionMetadata): ParseFnsource
Returns a parser that instantiates its object using Object.create with the prototype given in the metadata, and sets fields one at a time. This is slightly slower but the only option when no constructor is available.
i16(x: any, t: ParserTracer): undefined | numbersource
Expects a number and validates that it is within the signed 16-bit range.
i32(x: any, t: ParserTracer): undefined | numbersource
Expects a number and validates that it is within the signed 32-bit range.
i64(x: any, t: ParserTracer): undefined | numbersource
Expects a number and validates that it is within the signed 64-bit range.
i8(x: any, t: ParserTracer): undefined | numbersource
Expects a number and validates that it is within the signed 8-bit range.
json(value: any, t: ParserTracer): anysource
JSON parses a string to a value.
This is just a wrapper around JSON.parse with exceptions routed into the given ParserTracer.
literal<T extends string>(value: T): ParseFnsource
Returns a parser that parses a string of a fixed value.
map<K, V>(parseKey: ParseFn, parseValue: ParseFn): ParseFnsource
Returns a parser that parsers an object to a Map. The keys and values of the map are formed by applying the given key and value parsing functions. If either parser returns undefined at any stage, the entire map is rejected.
nullable<T>(parseFn: ParseFn): ParseFnsource
Wraps the given parse function so that it accepts and passes through null and undefined.
object(value: any, t: ParserTracer): undefined | objectsource
Parses an object, without performing any validation of its contents.
oneOf<T extends TypeMap>(parserMap: ParserMap<T>): ParseFnsource
Returns a parser that parses the serialised representation of a sum type to its equivalent in-memory value. The serialised form may be one of the following:
Literal: primitive types (
boolean,number,string, ornull) may be directly stored in the field.Tagged: for a struct type, the value is stored as an object with a single entry. The key of the entry is the 'tag' of the type; this is either explicitly defined by the model author, or defaults to the name of the struct type.
Each type may appear at most once in the tag map.
set<T>(elementParser: ParseFn): ParseFnsource
Returns a parser that parses an array to a Set, applying the given elementParser to each element. The set will be rejected if elementParser returns undefined for any element for any element of the array.
str(x: any, t: ParserTracer): undefined | stringsource
Returns a parser that parses a string.
timestamp(x: any, t: ParserTracer): undefined | Instantsource
Parses a string in ISO8601 timestamp format into a Temporal.Instant.
tuple<Ts extends any[]>(parsers: ParserTuple<Ts>): ParseFnsource
Returns a parser that parses an fixed-size array using a bijective mapping of parser to value. This differs from array in that the former is strictly homogenous, while a tuple may be heterogenous.
The array is rejected if:
- its length differs from the expected length; or
- any element parsing function returns
undefined.
u16(x: any, t: ParserTracer): undefined | numbersource
Expects a number and validates that it is within the unsigned 16-bit range.
u32(x: any, t: ParserTracer): undefined | numbersource
Expects a number and validates that it is within the unsigned 32-bit range.
u64(x: any, t: ParserTracer): undefined | numbersource
Expects a number and validates that it is within the unsigned 64-bit range.
u8(x: any, t: ParserTracer): undefined | numbersource
Expects a number and validates that it is within the unsigned 8-bit range.
unwrappedTuple<Ts extends any[]>(parsers: ParserTuple<Ts>): ParseFnsource
Returns a parser that parses either a fixed-size array or a single value. This is the same as tuple, except that 1-tuples are not expected to be enclosed in an array.
uuid(x: any, t: ParserTracer): undefined | uuidsource
Parses a string in UUIDv4 format into a uuid.
yearMonth(x: any, t: ParserTracer): undefined | PlainYearMonthsource
Parses a string in ISO8601 year-month format (YYYY-MM) into a Temporal.PlainYearMonth.