Skip to content

nullable ​

Allows null. Everything else is passed to the inner schema.

Signature ​

ts
function nullable<T>(schema: Schema<T>): Schema<T | null>

Parameters ​

ParameterTypeDescription
schemaSchema<T>The schema for the value when it is not null

Notes ​

  • undefined and '' still give a required error. To allow a missing field too: optional(nullable(...)).
  • data contains null — handy for clearing a column in the database.

Example ​

Clearing a field ​

ts
const UpdateUser = object({
  avatar: optional(nullable(string())),
});

validate(UpdateUser, {}).data;               // {} — leave the field as is
validate(UpdateUser, { avatar: null }).data; // { avatar: null } — remove the avatar

See also ​