ChatiumFor developersPlaygroundPricing
Sign in

Heap.Record

Declares an object field that models a "map" from one specified type (keys) to another (values). It is analogous to the Record type in Typescript.

Usage / Signature

import { Heap } from '@app/heap'
Heap.Record(keySchema, valueSchema, options)

Arguments

  • <b id="keySchema">keySchema</b>: HsString | HsNumber | HsKeyOf | HsUnion <b style="color:red">*</b>
    A schema describing the key type.

    This can be a schema for strings, numbers, or a schema for any union of string or numeric literals. In any case, only simple strings and numbers are supported as keys.

    Schemas with modifiers Heap.Optional and Heap.NonRequired are not supported and will result in a runtime error, as they do not make sense in this context.

  • <b id="valueSchema">valueSchema</b>: HeapSchema <b style="color:red">*</b>
    Values can be any supported schemas that can be declared using the object functions of Heap (except those that declare/modify entire tables).

    Schemas with modifiers Heap.Optional and Heap.NonRequired are not supported and will result in a runtime error, as they do not make sense in this context.

  • <b id="options">options</b>: {default}
    An object with additional optional parameters for the field.

    • <b id="default">default</b>: object
      The default value for this field. For more information on how default values work, see Guide / Heap / Default Values.

    Modifying the additionalProperties property from the JSON schema is not supported. If an object with keys that do not match the schema is provided, a validation error will occur.

Return Value

A JSON schema for an object with additionalProperties=false and patternProperties corresponding to the specified key and value schemas.

Examples

Simple map from string to number
const ratesHistory = Heap.Table('ratesHistory', {
  rates: Heap.Record(Heap.String(), Heap.Number()),
})
Keys restricted by a regular expression
const seaBattleMaps = Heap.Table('seaBattleMaps', {
  matrix: Heap.Record(Heap.RegEx(/^[a-j][0-9]$/), Heap.Boolean()),
})
Keys defined from the schema of another object
const formSchema = Heap.Object({
  field1: Heap.String(),
  field2: Heap.Number(),
})
const quizes = Heap.Table('quizes', {
  fieldsState: Heap.Record(Heap.KeyOf(formSchema), Heap.Boolean()),
})
Keys defined through Heap.Union
const ratesHistory = Heap.Table('ratesHistory', {
  rates: Heap.Record(
    Heap.Union([Heap.Literal('USD'), Heap.Literal('EUR')]),
    Heap.Money(),
  ),
})