fallback
Returns a fallback value as output if the input does not match the schema.
const Schema = v.fallback<TSchema, TFallback>(schema, fallback);
Generics
TSchemaextends BaseSchema<unknown, unknown, BaseIssue<unknown>>TFallbackextends Fallback<TSchema>
Parameters
schemaTSchemafallbackTFallback
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
nullorundefinedinputs, you should useoptional,nullableornullishinstead.The fallback value is not validated. Make sure that the fallback value matches your schema.
Returns
SchemaSchemaWithFallback<TSchema, TFallback>
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
fallbackparameter, the schema will return a newDateinstance each time the input does not match the schema.
const FallbackDateSchema = v.fallback(v.date(), () => new Date());
Related
The following APIs can be combined with fallback.
Schemas
any, array, bigint, blob, boolean, custom, date, enum, exactOptional, file, function, instance, intersect, lazy, literal, looseObject, looseTuple, map, nan, never, nonNullable, nonNullish, nonOptional, null, nullable, nullish, number, object, objectWithRest, optional, picklist, promise, record, set, strictObject, strictTuple, string, symbol, tuple, tupleWithRest, undefined, undefinedable, union, unknown, variant, void
Methods
assert, config, getDefault, getDefaults, getFallback, getFallbacks, is, keyof, message, omit, parse, parser, partial, pick, pipe, required, safeParse, safeParser, unwrap
Actions
args, base64, bic, brand, bytes, check, checkItems, codePoints, creditCard, cuid2, decimal, description, digits, domain, email, emoji, empty, endsWith, entries, everyItem, excludes, filterItems, findItem, finite, flavor, graphemes, gtValue, guard, hash, hexadecimal, hexColor, imei, includes, integer, ip, ipv4, ipv6, isbn, isrc, isoDate, isoDateTime, isoDateTimeSecond, isoTime, isoTimeSecond, isoTimestamp, isoWeek, ksuid, length, ltValue, mac, mac48, mac64, mapItems, maxBytes, maxCodePoints, maxEntries, maxGraphemes, maxLength, maxSize, maxValue, maxWords, metadata, mimeType, minBytes, minCodePoints, minEntries, minGraphemes, minLength, minSize, minValue, minWords, multipleOf, nanoid, nonEmpty, notBytes, notCodePoints, notEntries, notGraphemes, notLength, notSize, notValue, notValues, notWords, octal, parseJson, partialCheck, rawCheck, rawTransform, readonly, reduceItems, regex, returns, rfcEmail, safeInteger, size, slug, someItem, sortItems, startsWith, stringifyJson, title, toCamelCase, toKebabCase, toLowerCase, toMaxValue, toMinValue, toPascalCase, toSnakeCase, toUpperCase, transform, trim, trimEnd, trimStart, ulid, url, uuid, value, values, words
Utils
© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/fallback/