Skip to main content
Version: 2.0.0

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.

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() for create-time validation and .reValidate() for update-time validation. If reValidate is omitted, the create validator is reused.

b.required("username")
.validate(validateUsername)
.reValidate(validateUsernameUpdate);

Readonly and ignore updates

  • .readonly() — permanently ignore the field during updates.
  • .ignoreUpdate(resolver) — ignore updates when the resolver returns true.

These are mutually exclusive.

Loading playground…

Required error

Use .requiredError() to customize the message when the field is missing at creation.

b.required("email").requiredError("Email is required").validate(validateEmail);

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 for create (and update if no reValidate).
reValidate(validator)Validator used during updates.
readonly()Permanently ignore updates.
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.