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.
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()andfield.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
vflag. 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 afield.list(). stepnumber- A number’s step, counted from
min. acceptstring- The
acceptoffield.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,tooLongand the rest, or a list’stooFewandtooMany. Each is a string, or a function that returns one.
Caveats
- A number, choice, date or time with
required: trueor adefaultdropsundefinedfrom its value type, as does a file withrequired: true. - A declaration that contradicts itself throws where it is written, as
field.<kind>: …: an option its kind does not take,minabovemax, adefaultits own constraints refuse. - A declaration’s type keeps
required: true,messagesand a boolean’sdefault: trueas 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.
read(declaration, input): ReadingParameters
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.
write(declaration, value): readonly string[] | nullParameters
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.
explain(issue): stringParameters
issueIssue- One of the
issuesreadreturned.
Returns
string — An English phrase such as is below 1.
Issue
Import from @k8ordo/field
One refusal read returned.
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
ValidityStateflag, ortooFewandtooMany. 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.
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.
type Field<Value = unknown, Data extends FieldData = FieldData>