Creates a nullable schema.

const Schema = nullable<TWrapped, TDefault>(wrapped, default_);


  • TWrapped extends BaseSchema
  • TDefault extends Input<TWrapped> | (() => Input<TWrapped> | undefined) | undefined


  • wrapped TWrapped
  • default_ TDefault


With nullable the validation of your schema will pass null inputs, and if you specify a default_ input value, the schema will use it if the input is null. For this reason, the output type may differ from the input type of the schema.

Note that nullable does not accept undefined as an input. If you want to accept undefined inputs, use optional, and if you want to accept null and undefined inputs, use nullish instead. Also, if you want to set a default output value for any invalid input, you should use fallback instead.



The following examples show how nullable can be used.

Nullable string schema

Schema that accepts string and null.

const NullableStringSchema = nullable(string(), "I'm the default!");

Nullable date schema

Schema that accepts Date and null.

By using a function as the default_ parameter, the schema will return a new Date instance each time the input is null.

const NullableDateSchema = nullable(date(), () => new Date());

Nullable entry schema

Object schema with a nullable entry.

const NullableEntrySchema = object({
  key: nullable(string()),

Unwrap nullable schema

Use unwrap to undo the effect of nullable.

const NullableNumberSchema = nullable(number());
const NumberSchema = unwrap(NullableNumberSchema);

The following APIs can be combined with nullable.