---
title: unionAsync
description: Creates an union schema.
source: /schemas/union/unionAsync.ts
contributors:
  - fabian-hiller
  - EltonLobo07
---

import { ApiList, Link, Property } from '~/components';
import { properties } from './properties';

# unionAsync

Creates an union schema.

> I recommend that you read the <Link href="/guides/unions/">unions guide</Link> before using this schema function.

```ts
const Schema = v.unionAsync<TOptions, TMessage>(options, message);
```

## Generics

- `TOptions` <Property {...properties.TOptions} />
- `TMessage` <Property {...properties.TMessage} />

## Parameters

- `options` <Property {...properties.options} />
- `message` <Property {...properties.message} />

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

- `Schema` <Property {...properties.Schema} />

## Examples

The following examples show how `unionAsync` can be used.

### User schema

Schema to validate a user's email or username.

```ts
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

<ApiList
  items={[
    '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

<ApiList items={['config', 'getDefault', 'getFallback']} />

### Actions

<ApiList
  items={[
    'args',
    'base64',
    'bic',
    'brand',
    'bytes',
    'check',
    'checkItems',
    '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',
    'maxEntries',
    'maxGraphemes',
    'maxLength',
    'maxSize',
    'maxValue',
    'maxWords',
    'metadata',
    'mimeType',
    'minBytes',
    'minEntries',
    'minGraphemes',
    'minLength',
    'minSize',
    'minValue',
    'minWords',
    'multipleOf',
    'nonEmpty',
    'notBytes',
    '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

<ApiList items={['entriesFromList', 'isOfKind', 'isOfType']} />

### Async

<ApiList
  items={[
    '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',
  ]}
/>
