nullish

Creates a nullish schema.

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

Generics

Parameters

  • wrapped TWrapped
  • default_ TDefault

Explanation

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

Note that nullish accepts undefined and null as an input. If you want to accept only null inputs, use nullable, and if you want to accept only undefined inputs, use optional 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 nullish can be used.

Nullish string schema

Schema that accepts string, undefined and null.

const NullishStringSchema = nullish(string(), "I'm the default!");

Nullish date schema

Schema that accepts Date, undefined and null.

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

const NullishDateSchema = nullish(date(), () => new Date());

Nullish entry schema

Object schema with a nullish entry.

const NullishEntrySchema = object({
  key: nullish(string()),
});

Unwrap nullish schema

Use unwrap to undo the effect of nullish.

const NullishNumberSchema = nullish(number());
const NumberSchema = unwrap(NullishNumberSchema);

The following APIs can be combined with nullish.

Schemas

Methods

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 dailydotdev
  • GitHub profile picture of ivan-mihalic
  • GitHub profile picture of KATT
  • GitHub profile picture of osdiab
  • GitHub profile picture of Thanaen
  • GitHub profile picture of ruiaraujo012
  • GitHub profile picture of hyunbinseo
  • GitHub profile picture of caegdeveloper