GizStack
View .mdOpen in ChatGPTOpen in Claude

gizlang/constraints

what a field's annotations ask of its value. One owner of these rules for every typed value: a record's form fields and stored rows (gizmodel), and a JSON value checked against a type (Checker), as typed state at paths does (gizlang/state). It is in the SDK so a small program checks its values with no data layer.

gizlang
import * as Constraints from "gizlang/constraints";

Types

MaxLength

gizlang
type MaxLength = { value: i64; }

The most Unicode characters a text may contain.

  • value: i64

Pattern

gizlang
type Pattern = { value: string; }

The whole text must match this regular expression.

  • value: string

Values

gizlang
type Values = { list: string; }

Comma-separated allowed strings. Annotation arguments are scalar literals.

  • list: string

Min

gizlang
type Min = { value: i64; }

A text's fewest characters, or a number's least value.

  • value: i64

Max

gizlang
type Max = { value: i64; }

A text's most characters, or a number's greatest value.

  • value: i64

Bounds

gizlang
type Bounds = { min: Option<i64>; max: Option<i64>; maxLength: Option<i64>; pattern: Option<string>; values: Vec<string>; }

A field's constraints, read from its annotations (boundsOf).

  • min: _gizlang_sdk_core.Option<i64>
  • max: _gizlang_sdk_core.Option<i64>
  • maxLength: _gizlang_sdk_core.Option<i64>
  • pattern: _gizlang_sdk_core.Option<string>
  • values: Vec<string>

Checker

gizlang
type Checker = { all: Vec<Rule>; root: i64; }

What a type T allows in a JSON value (checkerOf), compiled once from its description (Memory.describe) so that a check walks the value alone: a record's fields, a union's "kind" and its case's fields, a Vec's or a Map's items, each field with its constraint annotations. An Option is its inner type; anything else (a scalar, an SDK type with a JSON form of its own) is open. Rules name each other by index, so a type that holds itself compiles once.

  • all: Vec<gizlang_constraints.Rule>
  • root: i64

Functions

boundsOf

gizlang
function boundsOf(field: Json): Bounds

The constraints that FIELD's annotations give: FIELD is a field as Memory.describe describes it.

Effects: fail, alloc.

checkerOf

gizlang
function checkerOf<T>(): Checker

The checker of T.

checkerFrom

gizlang
function checkerFrom(description: bytes): Checker

The checker of the type DESCRIPTION describes (Memory.describe).

Effects: fail, alloc.

Methods

Bounds.invalid

gizlang
function Bounds.invalid(self: Bounds): string

Whether a field's annotations name a pattern that is not a regular expression: the reason, or "".

Effects: fail, alloc.

Bounds.text

gizlang
function Bounds.text(self: Bounds, label: string, text: string): string

Why TEXT breaks the bounds, after LABEL, or "": its count of Unicode characters against the least, most and longest, the pattern (an empty text meets it, as in HTML: whether it may be empty is the least's) and the values.

Effects: fail, alloc.

Bounds.number

gizlang
function Bounds.number(self: Bounds, label: string, below: (bound: i64) => bool, above: (bound: i64) => bool): string

Why a number breaks @Min or @Max, after LABEL, or "". BELOW and ABOVE tell whether it is below or above a whole bound, compared exactly for its kind (an integer, a float, a decimal).

Effects: fail, read, write, console, io, alloc.

Bounds.allowed

gizlang
function Bounds.allowed(self: Bounds, label: string, text: string): string

Why TEXT is not one of @Values, after LABEL, or "".

Effects: fail, alloc.

Checker.check

gizlang
function Checker.check(self: Checker, value: Json): string

Why VALUE breaks T's fields or their constraints, after the field's path ("levels[0].value: 100 is the maximum."), or "". A field T lacks is refused; whether VALUE reads as T at all is the reader's (readJson).

Effects: fail, read, write, console, io, alloc.