Skip to main content
Version: 2.2.0

Schema Options

The second argument to new Schema((b) => ..., { ... }) configures schema-wide behavior. These options apply to every create, update, and delete operation.

note

Schema-level checks for postValidate run after individual field validators. Cross-field required constraints run during the required-field evaluation phase, before per-field validation. See Life cycles and Validators for more on execution order.

equalityDepth​

How deep nested objects are compared key by key when ivo checks whether an update changes a value. Default: 1.

Values are always compared by content: an equal copy is never a change. Below the depth, nested objects are compared by their serialized form, where the order of their keys matters. So when the same data can come back with keys in another order (from a database, say), a depth that's too low makes it look changed: the update goes through, and dependents and onSuccess handlers run for nothing.

Loading playground…

sanitizeError​

Transform the error payload before it is returned from create or update. When it returns another shape, declare that shape as the schema's fifth type argument, after the error metadata (the fourth).

Loading playground…

onDelete​

Global listener(s) invoked by model.delete. Receives the full entity and context options.

since v2.1.0 Listeners receive frozen copies of the entity and options: the objects passed to model.delete are no longer frozen themselves.

Loading playground…

onSuccess​

Global listener(s) invoked after a successful create or update. Can be a function or a grouped config that only runs when one of the listed fields is involved.

Loading playground…

postValidate​

Run cross-field validation after individual field validators have finished. Each config needs at least two fields and a validator.

The validator receives the operation context and may return undefined/true/void for success, or an object mapping field names to errors. It can also return sanitized values under the validated key for each field.

It only runs when at least one of its fields is provided. On update, read the fields from values (the stored values merged with the changes): input only holds the changes.

Loading playground…

Returning values​

Loading playground…

When none of its fields is provided​

Loading playground…

ignore​

Ignore input fields when the handler returns true. Only lax and virtual fields can be ignored at the schema level.

Loading playground…

ignoreUpdate​

Ignore update values for the listed fields when the handler returns true. Works with lax, required, and virtual fields.

Loading playground…

required​

Cross-field required constraint for lax and virtual fields. Use it when a field is only required depending on the value of another field.

The handler only runs when none of its fields is provided. It receives the operation context and returns an object mapping field names to errors, or undefined when no field is required.

Loading playground…

timestamps​

Enable createdAt and updatedAt automatically.

  • true — uses createdAt and updatedAt keys.
  • { createdAt?: boolean | string, updatedAt?: boolean | string | { key?: string, nullable?: boolean } } — customize key names or disable one of them.

updatedAt is nullable by default on create.

Loading playground…

Summary​

new Schema((b) => ..., {
equalityDepth: 1,
sanitizeError: (payload, ctxOptions) => payload,
onDelete: [listener],
onSuccess: [listener],
postValidate: { fields: ["a", "b"], validator: ... },
ignore: { fields: ["secret", "apiKey"], handler: () => true },
ignoreUpdate: { fields: ["email", "phone"], handler: () => true },
required: { fields: ["email", "phone"], handler: ... },
timestamps: true,
});
OptionDescription
equalityDepthNesting depth for value comparisons during updates.
sanitizeErrorTransform the error payload before returning it.
onDeleteGlobal listener(s) for model.delete.
onSuccessGlobal listener(s) after a successful create/update.
postValidateCross-field validation after per-field validators.
ignoreIgnore input fields when the handler returns true.
ignoreUpdateIgnore update values for the listed fields when the handler returns true.
requiredCross-field required constraint for lax/virtual fields.
timestampsEnable createdAt/updatedAt automatically.