Skip to content

Commit e775a63

Browse files
committed
Add content to unions guide on website
1 parent 231bac3 commit e775a63

File tree

1 file changed

+67
-1
lines changed
  • website/src/routes/guides/(schemas)/unions

1 file changed

+67
-1
lines changed
Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,75 @@
11
---
22
title: Unions
3+
description: >-
4+
In relational algebra, an union represents the conjunction of two sets. Simply
5+
put, it is a logical OR relationship. You can apply this concept to schemas.
36
contributors:
47
- fabian-hiller
58
---
69

10+
import { Link } from '@builder.io/qwik-city';
11+
712
# Unions
813

9-
> The content of this page is not yet ready. Check back in a few days.
14+
In relational algebra, an union represents the conjunction of two sets. Simply put, it is a logical OR relationship. You can apply this concept to schemas with <Link href="/api/union/">`union`</Link> and <Link href="/api/variant/">`variant`</Link>. For [discriminated unions](https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes-func.html#discriminated-unions) you use <Link href="/api/variant/">`variant`</Link> and in all other cases you use <Link href="/api/union/">`union`</Link>.
15+
16+
## Union schema
17+
18+
The schema function <Link href="/api/union/">`union`</Link> creates an OR relationship between any number of schemas that you pass as the first argument in the form of an array. On validation, the schema returns the result of the first schema that was successfully validated.
19+
20+
```ts
21+
import * as v from 'valibot';
22+
23+
const UnionSchema = v.union([v.string(), v.number()]); // string | number
24+
```
25+
26+
If the input does not match any of the schemas, an issue is returned that contains the issues of each schema as subissues. This is a special case within the library, as the issues of <Link href="/api/union/">`union`</Link> can contradict each other.
27+
28+
```ts
29+
[
30+
{
31+
reason: 'type',
32+
validation: 'union',
33+
origin: 'value',
34+
message: 'Invalid type',
35+
input: null,
36+
issues: [
37+
{
38+
reason: 'type',
39+
validation: 'string',
40+
origin: 'value',
41+
message: 'Invalid type',
42+
input: null,
43+
},
44+
{
45+
reason: 'type',
46+
validation: 'number',
47+
origin: 'value',
48+
message: 'Invalid type',
49+
input: null,
50+
},
51+
],
52+
},
53+
];
54+
```
55+
56+
## Variant schema
57+
58+
For better performance, more type safety, and a more targeted output of issues, you can use <Link href="/api/variant/">`variant`</Link> for discriminated unions. Therefore, I recommend using <Link href="/api/variant/">`variant`</Link> over <Link href="/api/union/">`union`</Link> whenever possible. A discriminated union is an OR relationship between objects that can be distinguished by a specific key.
59+
60+
When you call the schema function, you first specify the discriminator key. This is used to determine the schema to use for validation based on the input. The object schemas, in the form of an array, follow as the second argument.
61+
62+
```ts
63+
import * as v from 'valibot';
64+
65+
const VariantScheme = v.variant('type', [
66+
v.object({
67+
type: v.literal('foo'),
68+
foo: v.string(),
69+
}),
70+
v.object({
71+
type: v.literal('bar'),
72+
bar: v.number(),
73+
}),
74+
]);
75+
```

0 commit comments

Comments
 (0)