Skip to content

Commit

Permalink
Add content to intersections guide on website
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Jan 12, 2024
1 parent 5812544 commit 8ff2166
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
41 changes: 40 additions & 1 deletion website/src/routes/guides/(schemas)/intersections/index.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
---
title: Intersections
description: >-
An intersection represents a logical AND relationship. You can apply this
concept to your schemas with intersect and partially with merge.
contributors:
- fabian-hiller
---

import { Link } from '@builder.io/qwik-city';

# Intersections

> The content of this page is not yet ready. Check back in a few days.
An intersection represents a logical AND relationship. You can apply this concept to your schemas with <Link href="/api/intersect/">`intersect`</Link> and partially with <Link href="/api/merge/">`merge`</Link>. For simple object schemas I recommend <Link href="/api/merge/">`merge`</Link> and in all other cases <Link href="/api/intersect/">`intersect`</Link>.

## Intersect schema

The schema function <Link href="/api/intersect/">`intersect`</Link> creates an AND relationship between any number of schemas that you pass as the first argument in the form of an array. To pass the validation, the validation of each schema passed must be successful. If this is the case, the schema merges the output of the individual schemas and returns the result. If the validation fails, the schema returns any issues that occurred.

```ts
import * as v from 'valibot';

// TypeScript
type Intersect = { foo: string } & { bar: number };

// Valibot
const IntersectSchema = v.intersect([
v.object({ foo: v.string() }),
v.object({ bar: v.number() }),
]);
```

## Merge method

Technically, there is a big difference between <Link href="/api/intersect/">`intersect`</Link> and <Link href="/api/merge/">`merge`</Link>. <Link href="/api/intersect/">`intersect`</Link> is a schema function that executes the passed schemas during validation. In contrast, <Link href="/api/merge/">`merge`</Link> is a <Link href="../methods/">method</Link> that merges the entries of the given object schemas during initialization to create a new object schema.

As a result, <Link href="/api/merge/">`merge`</Link> usually has better performance than <Link href="/api/intersect/">`intersect`</Link> when validating unknown data. Also, subsequent object properties overwrite the previous ones. This is not the case with <Link href="/api/intersect/">`intersect`</Link>, since the validation would fail if two properties with the same name are fundamentally different. However, this also means that the `pipe` and `rest` parameter of the passed object schemas must be ignored and redefined.

```ts
import * as v from 'valibot';

const ObjectSchema = v.merge([
v.object({ foo: v.string(), baz: v.number() }),
v.object({ bar: v.string(), baz: v.boolean() }),
]); // { foo: string; bar: string; baz: boolean; }
```

In the previous code example, the `baz` property of the first object schema is overwritten by the `baz` property of the second object schema.
12 changes: 8 additions & 4 deletions website/src/routes/guides/(schemas)/unions/index.mdx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: Unions
description: >-
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.
An union represents a logical OR relationship. You can apply this concept to
your schemas with union and variant.
contributors:
- fabian-hiller
---
Expand All @@ -11,7 +11,7 @@ import { Link } from '@builder.io/qwik-city';

# Unions

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>.
An union represents a logical OR relationship. You can apply this concept to your 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>.

## Union schema

Expand All @@ -20,7 +20,11 @@ The schema function <Link href="/api/union/">`union`</Link> creates an OR relati
```ts
import * as v from 'valibot';

const UnionSchema = v.union([v.string(), v.number()]); // string | number
// TypeScript
type Union = string | number;

// Valibot
const UnionSchema = v.union([v.string(), v.number()]);
```

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.
Expand Down

0 comments on commit 8ff2166

Please sign in to comment.