fallback

Returns a fallback value as output if the input does not match the schema.

const Schema = v.fallback<TSchema, TFallback>(schema, fallback);

Generics

Parameters

  • schema TSchema
  • fallback TFallback

Explanation

fallback allows you to define a fallback value for the output that will be used if the validation of the input fails. This means that no issues will be returned when using fallback and the schema will always return an output.

If you only want to set a default value for null or undefined inputs, you should use optional, nullable or nullish instead.

The fallback value is not validated. Make sure that the fallback value matches your schema.

Returns

Examples

The following examples show how fallback can be used.

Fallback string schema

Schema that will always return a string output.

const FallbackStringSchema = v.fallback(v.string(), "I'm the fallback!");

Fallback date schema

Schema that will always return a Date output.

By using a function as the fallback parameter, the schema will return a new Date instance each time the input does not match the schema.

const FallbackDateSchema = v.fallback(v.date(), () => new Date());

The following APIs can be combined with fallback.

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 morinokami
  • GitHub profile picture of sqmasep
  • GitHub profile picture of FlorianDevPhynix

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