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.
import * as Constraints from "gizlang/constraints";Types
MaxLength
type MaxLength = { value: i64; }The most Unicode characters a text may contain.
value:i64
Pattern
type Pattern = { value: string; }The whole text must match this regular expression.
value:string
Values
type Values = { list: string; }Comma-separated allowed strings. Annotation arguments are scalar literals.
list:string
Min
type Min = { value: i64; }A text's fewest characters, or a number's least value.
value:i64
Max
type Max = { value: i64; }A text's most characters, or a number's greatest value.
value:i64
Bounds
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
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
function boundsOf(field: Json): BoundsThe constraints that FIELD's annotations give: FIELD is a field as Memory.describe describes it.
Effects: fail, alloc.
checkerOf
function checkerOf<T>(): CheckerThe checker of T.
checkerFrom
function checkerFrom(description: bytes): CheckerThe checker of the type DESCRIPTION describes (Memory.describe).
Effects: fail, alloc.
Methods
Bounds.invalid
function Bounds.invalid(self: Bounds): stringWhether a field's annotations name a pattern that is not a regular expression: the reason, or "".
Effects: fail, alloc.
Bounds.text
function Bounds.text(self: Bounds, label: string, text: string): stringWhy 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
function Bounds.number(self: Bounds, label: string, below: (bound: i64) => bool, above: (bound: i64) => bool): stringWhy 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
function Bounds.allowed(self: Bounds, label: string, text: string): stringWhy TEXT is not one of @Values, after LABEL, or "".
Effects: fail, alloc.
Checker.check
function Checker.check(self: Checker, value: Json): stringWhy 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.