fallback

Returns a fallback output value when validating the passed schema failed.

const Schema = 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.

Warning: 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 = fallback(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 = fallback(date(), () => new Date());

The following APIs can be combined with fallback.

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
  • 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 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