nullable
Allows null. Everything else is passed to the inner schema.
Signature
ts
function nullable<T>(schema: Schema<T>): Schema<T | null>Parameters
| Parameter | Type | Description |
|---|---|---|
schema | Schema<T> | The schema for the value when it is not null |
Notes
undefinedand''still give arequirederror. To allow a missing field too:optional(nullable(...)).datacontainsnull— 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