Skip to content

std/facts

std/facts

Import from @norbital-ai/std/facts

std/facts (§3.7): runtime-declared fields. An administrator declares the keys a record may carry (a jurisdiction's statutory facts, an intake form's questions) as rows; the record stores the values in one json field, and the collection's transform validates them with validateFacts. facts checks the declarations themselves.

FactSpec type

type FactSpec = {
    readonly key: string;
    readonly type: 'boolean' | 'number' | 'string';
    readonly label?: string | null;
    readonly description?: string | null;
    /** Required on a complete record; an incomplete one may still be saved. */
    readonly required?: boolean | null;
    /** A documented default; omission is not a declaration. */
    readonly default_value?: boolean | number | string | null;
    readonly options?: readonly (boolean | number | string)[] | null;
    readonly minimum?: number | null;
    readonly maximum?: number | null;
    readonly integer?: boolean | null;
    readonly min_length?: number | null;
};

One declared key, as stored (a json row or a seed file).

Members

NameTypeDescription
keystring
type'boolean' | 'number' | 'string'
label?string | null
description?string | null
required?boolean | null

Required on a complete record; an incomplete one may still be saved.

default_value?boolean | number | string | null

A documented default; omission is not a declaration.

options?readonly (boolean | number | string)[] | null
minimum?number | null
maximum?number | null
integer?boolean | null
min_length?number | null

Source: packages/std/src/facts.ts:6

facts function

function facts(specs: readonly FactSpec[]): readonly FactSpec[]

The declarations, checked: keys are identifiers and unique, constraints fit the type, options and default are valid values. Throws the first fault.

Parameters

NameTypeDescription
specsreadonly FactSpec[]

Source: packages/std/src/facts.ts:91

validateFacts function

function validateFacts(specs: readonly FactSpec[], values: { readonly [key: string]: unknown; }, o?: { complete?: boolean; }): string | null

The first fault of values against specs, or null: an undeclared key, a value of the wrong type or outside its constraints, and (with complete) a required fact that is missing or blank. A transform refuses with the message.

Parameters

NameTypeDescription
specsreadonly FactSpec[]
values{ readonly [key: string]: unknown; }
o?{ complete?: boolean; }

Source: packages/std/src/facts.ts:108