API

The functions and types @k8ordo/field exports, all imported from @k8ordo/field.

On this page

field

Import from @k8ordo/field

Declares one value: the builder names its kind, and the options its constraints and default. What it returns is frozen data.

ts
field.string(options?)    field.email(options?)
field.url(options?)       field.tel(options?)
field.password(options?)
field.number(options?)    field.integer(options?)
field.boolean(options?)   field.enum(values, options?)
field.date(options?)      field.time(options?)
field.datetime(options?)  field.file(options?)
field.list(item, options?)
field.object(fields)

Fields

requiredboolean
Refuses an absent value as valueMissing. field.list() and field.object() do not take it.
defaultvalue
What an absent value reads as.
minLength / maxLengthnumber
The length of a text kind, counted in UTF-16 code units.
patternstring
A regular expression a text kind’s whole value must match, read with the v flag.
min / maxnumber | string
A number’s range, a date’s or a time’s range (a string such as "2026-01-01"), or the item count of a field.list().
stepnumber
A number’s step, counted from min.
acceptstring
The accept of field.file(). It becomes the form control’s attribute; the type is not checked.
messagesFieldMessages
A message per reason a value is refused, keyed by valueMissing, tooLong and the rest, or a list’s tooFew and tooMany. Each is a string, or a function that returns one.

Caveats

  • A number, choice, date or time with required: true or a default drops undefined from its value type, as does a file with required: true.
  • A declaration that contradicts itself throws where it is written, as field.<kind>: …: an option its kind does not take, min above max, a default its own constraints refuse.
  • A declaration’s type keeps required: true, messages and a boolean’s default: true as given, which is how a package that takes declarations refuses one by type.

read

Import from @k8ordo/field

Reads what a place holds the way the declaration says. It never throws.

ts
read(declaration, input): Reading

Parameters

declarationFieldData
The declaration to read with.
inputunknown
A string, a list of strings, or the value itself.

Returns

{ value: unknown; issues: readonly Issue[] } — The value, with every refused part replaced by its default, and the issues.

write

Import from @k8ordo/field

Spells a value as the strings a URL parameter holds.

ts
write(declaration, value): readonly string[] | null

Parameters

declarationFieldData
The declaration to read with.
valueunknown
The value to spell.

Returns

readonly string[] | null — None at the default, one per item of a list, and null for a value the declaration cannot spell.

explain

Import from @k8ordo/field

Words a refusal in English for the developer, without the refused value.

ts
explain(issue): string

Parameters

issueIssue
One of the issues read returned.

Returns

string — An English phrase such as is below 1.

Issue

Import from @k8ordo/field

One refusal read returned.

ts
type Issue = { path; flag; field }

Fields

pathReadonlyArray<string | number>
The keys and list indexes from the value read down to the one refused.
flagFlag
Why it was refused: a ValidityState flag, or tooFew and tooMany.
fieldFieldData
The declaration that refused it.

ValuesOf

Import from @k8ordo/field

The type of the values a record of declarations reads. For one declaration, use ValueOf.

ts
type ValuesOf<Fields>
type ValueOf<F>

Field

Import from @k8ordo/field

The type of a declaration: the value it reads, and the data it is. Use it for a parameter that takes a declaration.

ts
type Field<Value = unknown, Data extends FieldData = FieldData>
k8ordo

React libraries that use Baseline features without holding back.