undefinedable

Creates an undefinedable schema.

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

Generics

Parameters

  • wrapped TWrapped
  • default_ TDefault

Explanation

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

undefinedable behaves exactly the same as optional at runtime. The only difference is the input and output type when used for object entries. While optional adds a question mark to the key, undefinedable does not.

Note that undefinedable does not accept null as an input. If you want to accept null inputs, use nullable, 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 undefinedable can be used.

Undefinedable string schema

Schema that accepts string and undefined.

const UndefinedableStringSchema = v.undefinedable(
  v.string(),
  "I'm the default!"
);

Undefinedable date schema

Schema that accepts Date and undefined.

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

const UndefinedableDateSchema = v.undefinedable(v.date(), () => new Date());

Undefinedable entry schema

Object schema with an undefinedable entry.

const UndefinedableEntrySchema = v.object({
  key: v.undefinedable(v.string()),
});

Unwrap undefinedable schema

Use unwrap to undo the effect of undefinedable.

const UndefinedableNumberSchema = v.undefinedable(v.number());
const NumberSchema = v.unwrap(UndefinedableNumberSchema);

The following APIs can be combined with undefinedable.

Schemas

Methods

Actions

Utils

Contributors

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

  • GitHub profile picture of fabian-hiller

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 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 caegdeveloper
  • GitHub profile picture of andrew-d-jackson
  • GitHub profile picture of dslatkin