Required Fields
A required field is both an input and output field whose value must be provided at creation.
You must call .validate() or .allow() before the field can be built.
Allowed values
.allow() is a concise alternative to a validator and satisfies the required-field rule.
Use .allowError() to customize the error message:
b.required("role")
.allow(["admin", "editor"])
.allowError((value, allowed) => `"${value}" is not a valid role`);
Validation
Use .validate() to validate the field on create and on update. .reValidate() adds a second
check that runs once the validator has passed, e.g. an async uniqueness check (see Re-validators).
Readonly and ignore updates
.ignoreUpdate(resolver)— ignore the field's updates while the resolver returnstrue..readonly()— ignore all of the field's updates: it keeps the value it was created with.
These are mutually exclusive.
ignoreUpdate
readonly
Required error
Use .requiredError() to customize the message when the field is missing at creation: a static
message, or a function of the creation context. Call it before .validate() or .allow().
Hooks
Required fields support onDelete, onSuccess, and onFailure listeners.
API summary
| Method | Description |
|---|---|
required(name) | Create a required field. |
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. |
readonly() | Ignore all updates: keep the created value. |
ignoreUpdate(resolver) | Ignore updates when resolver returns true. |
requiredError(error) | Customize the missing-field error. |
onDelete(handler) | Listener invoked by model.delete. |
onFailure(handler) | Listener invoked after validation failure. |
onSuccess(handler) | Listener invoked after a successful create/update. |