Skip to main content
Version: 2.2.0

Lax Fields

laxinput and output

A lax field is both an input and output field whose value may or may not be provided at creation. When missing, its default value is used.

Loading playground…

Allowed values​

Restrict a lax field to a fixed set of values with .allow(). The array must contain at least two values, and the static default must be one of them.

Loading playground…

Use .allowError() to customize the error message:

b.lax("status", "draft")
.allow(["draft", "published"])
.allowError((value, allowed) => `"${value}" is not a valid status`);

Default values​

The default value can be static or a resolver function. Resolvers receive the creation context:

b.lax("timezone", "UTC");
b.lax("locale", ({ options }) => options.locale ?? "en");

If a resolver throws, the field value becomes null.

Validation​

Use .validate() to validate the field on create and on update. .reValidate() adds a second check that runs once the validator has passed (see Re-validators).

Loading playground…

Conditional required​

A lax field can be made conditionally required with .required(handler). The handler receives the operation context and returns a tuple [isRequired, errorMessage].

Loading playground…

.required() is mutually exclusive with .readonly(), .ignoreInit(), and .ignoreUpdate().

Readonly​

readonly() is only available when the default is a static value. It locks the field once its current value has diverged from the default.

Loading playground…

.readonly() is mutually exclusive with .required(), .ignoreInit(), and .ignoreUpdate().

Ignore rules​

An ignore rule drops a provided value, so the field keeps its current value (its default on create):

  • .ignore(resolver) — on create and on update, when the resolver returns true.
  • .ignoreInit() — always on create: the field can only be set by an update.
  • .ignoreUpdate() — always on update: the field can only be set on create.

These are mutually exclusive with .readonly() and .required().

ignore​

Loading playground…

ignoreInit​

Loading playground…

ignoreUpdate​

Loading playground…

Hooks​

Lax fields support onDelete, onSuccess, and onFailure listeners.

API summary​

MethodDescription
lax(name, defaultValue)Create a lax field with a static default or resolver.
allow(values)Restrict to at least two allowed values.
allowError(error)Customize the not-allowed error.
validate(validator)Validator, on create and update.
reValidate(validator)Second check, once validate has passed.
required(handler)Conditionally require the field.
readonly()Lock after divergence; only with a static default.
ignore(resolver)Ignore field when resolver returns true.
ignoreInit()Ignore field at creation.
ignoreUpdate()Ignore field at update.
onDelete(handler)Listener invoked by model.delete.
onFailure(handler)Listener invoked after validation failure.
onSuccess(handler)Listener invoked after a successful create/update.