Skip to main content
Version: 2.2.0

Life Cycles

ivo exposes hooks at different stages of an operation.

Operation context​

Validators, resolvers and listeners receive a context object (ctx) with the operation's state:

{
input: Partial<Input>; // sanitized input values
rawInput: Partial<Input>; // original input values
values: Output; // current values (on update: previous values merged with the changes)
isUpdate: boolean;
changes: Partial<Output> | null; // on update, the changes; null on create
previousValues: Output | null; // on update, the values before the update; null on create
options: CtxOptions; // a fresh copy on every read
updateOptions: (options: Partial<CtxOptions>) => void; // not given to listeners
}

On create and on update:

Loading playground…

Constant, default and ignoreUpdate resolvers receive a smaller context (input, rawInput, options and updateOptions, plus values for constants and previousValues for ignoreUpdate).

The context is frozen, and its properties are getters. Read them directly or destructure them:

.validate((value, ctx) => {
const { isUpdate, values } = ctx; // ✅
// ...
});

since v2.1.0 The getters are defined on the context's prototype, not on the object itself, so { ...ctx }, Object.keys(ctx) and JSON.stringify(ctx) no longer list them. Copy the properties you need instead, e.g. { values: ctx.values, isUpdate: ctx.isUpdate }.

Global listeners​

Set them on schema options:

Loading playground…

Field listeners​

Field builders support onSuccess, onFailure, and onDelete. create and update don't run them: they return handleSuccess (on success) or handleFailure (on failure), to call once the operation's result is in place (e.g. saved).

onSuccess​

Runs for the fields involved: every field on create, the changed ones on update.

Loading playground…

onFailure​

Runs for the fields that were provided.

Loading playground…

Post-validation​

Use postValidate to run cross-field validation after individual field validators:

new Schema((b) => ..., {
postValidate: {
fields: ['email', 'phoneNumber'],
validator: ({ input }) => {
if (!input.email && !input.phoneNumber)
return { email: "Provide an email or a phone number" };
},
},
});

Custom context options​

Pass extra data through every operation, as the schema's third type argument and the last argument of create / update / delete. Validators, resolvers and listeners read it as ctx.options; those that receive updateOptions can change it, for the handlers that run after them and for the caller, who gets the final options back as options.

Loading playground…