exactOptional

Creates an exact optional schema.

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

Generics

Parameters

  • wrapped TWrapped
  • default_ TDefault

Explanation

With exactOptional the validation of your schema will pass missing object entries, and if you specify a default_ input value, the schema will use it if the object entry is missing. For this reason, the output type may differ from the input type of the schema.

The difference to optional is that this schema function follows the implementation of TypeScript's exactOptionalPropertyTypes configuration and only allows missing but not undefined object entries.

Returns

Examples

The following examples show how exactOptional can be used.

Exact optional object entries

Object schema with exact optional entries.

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

const OptionalEntrySchema = v.object({
  key1: v.exactOptional(v.string()),
  key2: v.exactOptional(v.string(), "I'm the default!"),
  key3: v.exactOptional(v.date(), () => new Date()),
});

Unwrap exact optional schema

Use unwrap to undo the effect of exactOptional.

const OptionalNumberSchema = v.exactOptional(v.number());
const NumberSchema = v.unwrap(OptionalNumberSchema);

The following APIs can be combined with exactOptional.

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 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 Unique-Pixels
  • GitHub profile picture of jdgamble555
  • GitHub profile picture of nickytonline
  • GitHub profile picture of KubaJastrz
  • GitHub profile picture of caegdeveloper
  • GitHub profile picture of akhmadqasim
  • GitHub profile picture of dslatkin