Primitives

Status: implemented

boolean, number, string and bigint each admit every value of that JavaScript kind. unknown admits any DataJS value.

SchemaAdmitsTs<>JSON Schema
booleantrue, falseboolean{ "type": "boolean" }
numberany number, NaN and the infinities includednumber{ "type": "number" }
stringany stringstring{ "type": "string" }
bigintany bigintbigint{ "type": "integer" }, lossy
unknownany DataJS valueUnknown, the DataJS value type{}

Each is a thunk returning a one-element tag, ['number'] for number, so a schema can tell a built-in from a constant without comparing values. A primitive and the union of all its constants are one set: or(true, false) and boolean are written differently and accept the same values, which the data form's equal confirms (unions).

Examples

The demo on this page runs the Primitives lesson: a struct of boolean, number, string, bigint and unknown against a value with NaN for the number and 7n for the bigint, written as DataJS. Put a number where the bigint is to see both readers refuse it. The lesson is defined in sample.f.mjs, and the module page's demo lists it among all the others.

Reference

Where it is implemented

The nullary schemas in module.f.mjs; the kind checks in common.

Demo

Each primitive schema admits every value of its kind, NaN among the numbers; unknown admits any DataJS value. Try a number where the bigint is.

{ flag: boolean, count: number, name: string, big: bigint, extra: unknown }
{}flagcountnamebigextrabooleannumberstringbigintunknown

TypeScript:

{readonly"big":bigint,readonly"count":number,readonly"extra":unknown,readonly"flag":boolean,readonly"name":string}

JSON Schema:

{"type":"object","properties":{"big":{"type":"integer"},"count":{"type":"number"},"extra":{"anyOf":[{"const":null},{"type":"boolean"},{"type":"number"},{"type":"string"},{"type":"integer"},{"type":"array"},{"type":"object"}]},"flag":{"type":"boolean"},"name":{"type":"string"}},"required":["big","count","extra","flag","name"],"additionalProperties":{"not":{}}}
Canonical form (toData)
export default [{},{"object":[{"props":{"big":{"bigint":true},"count":{"number":true},"extra":{"unit":15,"number":true,"string":true,"bigint":true,"array":true,"object":true},"flag":{"unit":12},"name":{"string":true}},"rest":{}}]}];

parse · ok

export default {"flag":true,"count":NaN,"name":"x","big":7n,"extra":[null]};

validate · ok

export default {"flag":true,"count":NaN,"name":"x","big":7n,"extra":[null]};

Emergent Testing

Idle. Press Run to start the suite.

  • ./proof.f.mjs