Lax Fields
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.
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.
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).
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].
.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.
.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 returnstrue..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
ignoreInit
ignoreUpdate
Hooks
Lax fields support onDelete, onSuccess, and onFailure listeners.
API summary
| Method | Description |
|---|---|
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. |