unionAsync
Creates an union schema.
I recommend that you read the unions guide before using this schema function.
const Schema = v.unionAsync<TOptions, TMessage>(options, message);
Generics
TOptionsextends UnionOptionsAsyncTMessageextends ErrorMessage<UnionIssue<InferIssue<TOptions[number]>>> | undefined
Parameters
optionsTOptionsmessageTMessage
Explanation
With unionAsync you can validate if the input matches one of the given options. If the input does not match a schema and cannot be clearly assigned to one of the options, you can use message to customize the error message.
If a bad input can be uniquely assigned to one of the schemas based on the data type, the result of that schema is returned. Otherwise, a general issue is returned that contains the issues of each schema as subissues. This is a special case within the library, as the issues of unionAsync can contradict each other.
Returns
SchemaUnionSchemaAsync<TOptions, TMessage>
Examples
The following examples show how unionAsync can be used.
User schema
Schema to validate a user's email or username.
import { isEmailPresent, isUsernamePresent } from '~/api';
const UserSchema = v.unionAsync([
v.pipeAsync(
v.string(),
v.email(),
v.checkAsync(isEmailPresent, 'The email is not in the database.')
),
v.pipeAsync(
v.string(),
v.nonEmpty(),
v.checkAsync(isUsernamePresent, 'The username is not in the database.')
),
]);
Related
The following APIs can be combined with unionAsync.
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
config, getDefault, getFallback
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, 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
entriesFromList, isOfKind, isOfType
Async
arrayAsync, awaitAsync, checkAsync, customAsync, exactOptionalAsync, fallbackAsync, getDefaultsAsync, getFallbacksAsync, intersectAsync, lazyAsync, looseObjectAsync, looseTupleAsync, mapAsync, nonNullableAsync, nonNullishAsync, nonOptionalAsync, nullableAsync, nullishAsync, objectAsync, objectWithRestAsync, optionalAsync, parseAsync, parserAsync, partialCheckAsync, pipeAsync, rawCheckAsync, rawTransformAsync, recordAsync, safeParseAsync, safeParserAsync, setAsync, strictObjectAsync, strictTupleAsync, transformAsync, tupleAsync, tupleWithRestAsync, variantAsync
© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/unionAsync/