Skip to main content
Version: 2.2.0

Required Fields

requiredinput and output

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.

Loading playground…

Allowed values​

.allow() is a concise alternative to a validator and satisfies the required-field rule.

Loading playground…

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).

Loading playground…

Readonly and ignore updates​

  • .ignoreUpdate(resolver) — ignore the field's updates while the resolver returns true.
  • .readonly() — ignore all of the field's updates: it keeps the value it was created with.

These are mutually exclusive.

ignoreUpdate​

Loading playground…

readonly​

Loading playground…

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().

Loading playground…

Hooks​

Required fields support onDelete, onSuccess, and onFailure listeners.

API summary​

MethodDescription
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.