Skip to main content
Version: 2.2.0

Virtual Fields

virtualinput only

A virtual field is a purely input field whose value may or may not be provided, used to trigger a change in one or more dependent fields. It is not part of the output.

A virtual field must have at least one dependent field that depends on it, and it must declare .allow() or .validate() before most other rules unlock.

Loading playground…

Allowed values​

.allow() restricts the accepted input values. As with .validate(), it unlocks the rest of the builder.

b.virtual("role").allow(["admin", "editor"]);

The array must contain at least two values. Use .allowError() to customize the error.

Validation​

.validate() is the most common way to unlock the virtual builder. The validator receives the raw input value and returns a validation result.

Loading playground…

Alias​

Use .alias(name) to accept the virtual value under a different input key: the name of a dependent field of the virtual, so callers provide the value under the output field's name. The value is then read under the alias in the context (ctx.input). .alias() can be set before or after .allow() / .validate(), but only once.

Loading playground…

Sanitizer​

Use .sanitize() to transform a virtual field's value once validation has passed, before dependents resolve. The sanitizer receives the operation context and returns the new value. Sanitizers can be declared once .allow() / .validate() has been.

Loading playground…

Conditional required​

A virtual field can be made conditionally required with .required(handler), just like a lax field.

Loading playground…

Ignore rules​

An ignore rule drops a provided virtual, so its dependents aren't triggered:

  • .ignore(resolver) — on create and on update, when the resolver returns true.
  • .ignoreInit() — always on create: the virtual only counts in updates.
  • .ignoreUpdate() — always on update: the virtual only counts on create.

These unlock only after .allow() or .validate().

ignore​

Loading playground…

ignoreInit​

Loading playground…

ignoreUpdate​

Loading playground…

Hooks​

Virtual fields support onSuccess and onFailure listeners.

API summary​

MethodDescription
virtual(name)Create a virtual input field.
alias(name)Accept the value under a different input key.
allow(values)Restrict to at least two allowed values.
allowError(error)Customize the not-allowed error.
validate(validator)Validator for the input value.
reValidate(validator)Second check, once validate has passed.
required(handler)Conditionally require the field.
sanitize(sanitizer)Transform the validated value before dependents.
ignore(resolver)Ignore field when resolver returns true.
ignoreInit()Ignore field at creation.
ignoreUpdate()Ignore field at update.
onFailure(handler)Listener invoked after validation failure.
onSuccess(handler)Listener invoked after a successful create/update.