Skip to main content
Version: 1.9.0

Dependents

Dependent properties are resolved automatically from other properties. Any external attempt to change their value is ignored; their value is solely modifiable through their resolver function.

Defining a dependent

A dependent field requires three rules:

  • default: a value or function used as (or to generate) the default value
  • dependsOn: at least one other property or virtual the field depends on
  • resolver: a sync/async function that produces the new value when a dependency changes
import { Schema, type IvoSummary } from "ivo";

type Input = {
firstName: string;
lastName: string;
};

type Output = {
firstName: string;
fullName: string;
lastName: string;
};

const userSchema = new Schema<Input, Output>({
firstName: { required: true, validator: validateName },
lastName: { required: true, validator: validateName },
fullName: {
default: "",
dependsOn: ["firstName", "lastName"],
resolver({ ctx: { firstName, lastName } }) {
return `${firstName} ${lastName}`;
},
},
});

Note: The resolver runs after post-validation and virtual sanitizers. If the resolver throws during creation, the value becomes null; during an update, the property is ignored.

Default values

Dependent properties must have a default value. This value is used when the resolver has not yet run or when no dependencies are provided.

const schema = new Schema({
total: {
default: 0,
dependsOn: ["price", "quantity"],
resolver({ ctx }) {
return ctx.price * ctx.quantity;
},
},
});

Readonly

Dependent properties can be made readonly with readonly: true. Once resolved, their value cannot be changed externally.

const schema = new Schema({
completedAt: {
default: "",
readonly: true,
dependsOn: "isComplete",
resolver({ ctx }) {
return ctx.isComplete ? new Date() : "";
},
},
});

Limitations

  • Dependent properties cannot be required.
  • They cannot have their own validator; validation should happen on the properties they depend on.

API summary

OptionTypeRequiredDescription
defaultany | functionYesDefault value or resolver for the property.
dependsOnstring | string[]YesProperty or properties the field depends on.
resolverfunctionYesFunction that computes the value when dependencies change.
readonlytrueNoPrevents external updates after the value is resolved.
onDeletefunction | function[]NoHandler(s) invoked when the model instance is deleted.
onSuccessfunction | function[]NoHandler(s) invoked after a successful create or update operation.