Schema Options
The second argument to new Schema((b) => ..., { ... }) configures schema-wide behavior. These options apply to every create, update, and delete operation.
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.
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).
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.
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.
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.
Returning values
When none of its fields is provided
ignore
Ignore input fields when the handler returns true. Only lax and virtual fields can be ignored at the schema level.
ignoreUpdate
Ignore update values for the listed fields when the handler returns true. Works with lax, required, and virtual fields.
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.
timestamps
Enable createdAt and updatedAt automatically.
true— usescreatedAtandupdatedAtkeys.{ 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.
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,
});
| Option | Description |
|---|---|
equalityDepth | Nesting depth for value comparisons during updates. |
sanitizeError | Transform the error payload before returning it. |
onDelete | Global listener(s) for model.delete. |
onSuccess | Global listener(s) after a successful create/update. |
postValidate | Cross-field validation after per-field validators. |
ignore | Ignore input fields when the handler returns true. |
ignoreUpdate | Ignore update values for the listed fields when the handler returns true. |
required | Cross-field required constraint for lax/virtual fields. |
timestamps | Enable createdAt/updatedAt automatically. |