Metadata
Each of the types above has an associated metadata function in
M.metadata()
, with some useful additions:const {
_,
base,
asIs,
any,
anyOf,
maybe,
withDefault,
number,
string,
boolean,
date,
wrappedNumber,
map,
enumMap,
stringMap,
list,
set
} = M.metadata()
eg.
_(Animal)
, _(M.List, [string()])
, _(M.Map, [date(), number()])
To retrieve the metadata of Modélico types
Retrieve the metadata of standard models for extension. See JSON validation (Overridingthe reviver) for an example.
Useful for simple transformations, eg.:
asIs(x => 2 * x)
to double numbers. It serves as base for primitive metadata and can be used to fix a value for debugging purposes, eg.: asIs(() => 0)
. It is up to you to implement toJSON()
on the instance to reverse the transformation if you need to.Leaves the JSON input untouched (same as
asIs(identity)
).Useful to revive polymorphic JSON based on an enumerated field. The field defaults to
type
if not provided. See Reviving polymorphic JSON: (example 1) for an example. Note that anyOf
does not return metadata, but a metadata-returning function. This is something you can use as well to revive some cases of polymorphic JSON.For native numbers
Wraps numbers with
ModelicoNumber
to support -0
, NaN
and ±Infinity
.For native numbers
For native booleans
eg.
map(date(), string())
For ordered maps with arbitrary keys and values with JSON representation
[[<key1>, <value1>], [<key2>, <value2>]]
eg.
enumMap(_(MyEnum), number())
For ordered maps with enum keys and arbitrary values with JSON representation
{"<enum1>": <value1>, "<enum2>": <value2>}
eg.
stringMap(date())
For ordered maps with string keys and arbitrary values with JSON representation
{"<string1>": <value1>, "<string2>": <value2>}
eg.
list(string())
; tuples: list([number(), string()])
For arrays. It has support for tuples in the sense of fixed-length arrays with per-item metadata.
eg.
set(number())
For ordered collections with no repeated values
eg.
maybe(string())
To declare a value as optional. Unless a value is a
Maybe
, it cannot be missing from the JSON or be null
. See Optional / null values: Maybes for more details.eg.
withDefault(number(), 100)
To set a default if the value is
null
or missing. See Optional / null values: using with default for more details.Last modified 2yr ago