nullable

Creates a nullable schema.

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

Generics

Parameters

  • wrapped TWrapped
  • default_ TDefault

Explanation

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.

Returns

Examples

The following examples show how nullable can be used.

Nullable string schema

Schema that accepts string and null.

const NullableStringSchema = v.nullable(v.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 = v.nullable(v.date(), () => new Date());

Nullable entry schema

Object schema with a nullable entry.

const NullableEntrySchema = v.object({
  key: v.nullable(v.string()),
});

Unwrap nullable schema

Use unwrap to undo the effect of nullable.

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

The following APIs can be combined with nullable.

Schemas

Methods

Actions

Utils

Contributors

Thanks to all the contributors who helped make this page better!

  • GitHub profile picture of fabian-hiller
  • GitHub profile picture of sqmasep

Partners

Thanks to our partners who support the project ideally and financially.

Sponsors

Thanks to our GitHub sponsors who support the project financially.

  • GitHub profile picture of antfu
  • GitHub profile picture of Thanaen
  • GitHub profile picture of osdiab
  • GitHub profile picture of ruiaraujo012
  • GitHub profile picture of hyunbinseo
  • GitHub profile picture of F0rce
  • GitHub profile picture of fabulousgk
  • GitHub profile picture of jdgamble555
  • GitHub profile picture of isoden
  • GitHub profile picture of nickytonline
  • GitHub profile picture of caegdeveloper
  • GitHub profile picture of luckasnix
  • GitHub profile picture of andrew-3kb
  • GitHub profile picture of dslatkin