Skip to main content
Version: 1.9.0

Required

Required properties must be provided during creation. They must have a validator and cannot have a default value when they are strictly required.

Defining a required field

import { Schema } from "ivo";

const userSchema = new Schema({
firstName: { required: true, validator: validateName },
lastName: { required: true, validator: validateName },
});

Validation

Required fields must have a validator. They may also have a reValidator for secondary validation.

const schema = new Schema({
email: {
required: true,
validator: validateEmail,
reValidator: makeSureEmailIsUnique,
},
});

Allowed values

Use allow to restrict the accepted values. The value is checked against the allowed list before being passed to the validator.

const schema = new Schema({
role: { required: true, allow: ["admin", "user"], validator: validateRole },
});

Custom errors work the same as for lax fields:

const schema = new Schema({
role: {
required: true,
allow: {
values: ["admin", "user"],
error: "Invalid role provided",
},
validator: validateRole,
},
});

Conditionally required

Instead of required: true, you can provide a function that decides whether the field is required for the current operation.

type RequiredError = string | { reason?: string; metadata?: object | null };

The function may return:

  • boolean
  • [boolean, RequiredError]
  • Promise<boolean | [boolean, RequiredError]>
import { Schema, type IvoSummary } from "ivo";

type Book = {
bookId: string;
isPublished: boolean;
price: number | null;
};

const bookSchema = new Schema<Book>({
bookId: { required: true, validator: validateBookId },
isPublished: { default: false, validator: validateBoolean },
price: {
default: null,
required({ ctx: { isPublished, price } }: IvoSummary<Book>) {
const isRequired = price == null && isPublished;
return [isRequired, "A price is required to publish a book!"];
},
validator: validatePrice,
},
});

Notes:

  • If no required error is provided, [propertyName] is required! is used.
  • If the required function returns nothing, the operation proceeds with required: false.
  • If the required function throws, the operation proceeds with required: false.

Conditionally required fields may have a default value.

Readonly

Required fields cannot be strictly readonly (readonly: true with required: true). However, a conditionally required field may be readonly.

Ignore rules

Required fields support ignore, ignoreInit, and ignoreUpdate.

Lifecycle hooks

Required fields support onDelete, onFailure, and onSuccess handlers.

API summary

OptionTypeRequiredDescription
requiredtrue | functionYesMarks the field as required or makes it conditionally required.
validatorfunctionYes*Primary validator. Required when required: true.
reValidatorfunctionNoSecondary validator.
allowany[] | objectNoAllowed values and optional custom error.
requiredErrorstring | functionNoCustom error for conditional requirement.
readonlytrue | 'lax'NoOnly allowed with conditional requirement.
ignorefunctionNoDetermines whether input should be ignored.
ignoreInittrueNoIgnores the field during creation.
ignoreUpdatetrueNoIgnores the field during updates.
onDeletefunction | function[]NoHandler(s) invoked when the model instance is deleted.
onFailurefunction | function[]NoHandler(s) invoked after a failed create or update operation.
onSuccessfunction | function[]NoHandler(s) invoked after a successful create or update operation.