For the complete documentation index, see llms.txt. This page is also available as Markdown.

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()

_(Type, innerMetadata = [])

eg. _(Animal), _(M.List, [string()]), _(M.Map, [date(), number()])

To retrieve the metadata of Modélico types

base(Type)

Retrieve the metadata of standard models for extension. See JSON validation (Overridingthe reviver) for an example.

asIs(transform = identity)

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.

any()

Leaves the JSON input untouched (same as asIs(identity)).

anyOf([[metadata1, enumValue1], ..., [metadataN, enumValueN]] = [], field = 'type')

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.

number()

For native numbers

wrappedNumber()

Wraps numbers with ModelicoNumber to support -0, NaN and ±Infinity.

string()

For native numbers

boolean()

For native booleans

date()

To parse date-time strings, as defined by RFC 3339, section 5.6, eg. 1978-12-06T00:00:00.000Z.

map(keyMetadata = any(), valueMetadata = any())

eg. map(date(), string())

For ordered maps with arbitrary keys and values with JSON representation

enumMap(enumMetadata, valueMetadata = any())

eg. enumMap(_(MyEnum), number())

For ordered maps with enum keys and arbitrary values with JSON representation

stringMap(valueMetadata = any())

eg. stringMap(date())

For ordered maps with string keys and arbitrary values with JSON representation

list(itemMetadata = any()), list([...itemMetadata])

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.

set(itemMetadata)

eg. set(number())

For ordered collections with no repeated values

maybe(metadata = any())

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.

withDefault(metadata, default)

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 updated