Skip to content

Commit

Permalink
Add content to intersect API reference on website
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Jan 19, 2024
1 parent c011cd6 commit c0831b0
Show file tree
Hide file tree
Showing 14 changed files with 472 additions and 6 deletions.
4 changes: 2 additions & 2 deletions library/src/schemas/intersect/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { IntersectOptions } from './intersect.ts';
import type { IntersectOptionsAsync } from './intersectAsync.ts';

/**
* Intersect input type.
* Intersect input inference type.
*/
export type IntersectInput<
TIntersectOptions extends IntersectOptions | IntersectOptionsAsync
Expand All @@ -21,7 +21,7 @@ export type IntersectInput<
: never;

/**
* Intersect output type.
* Intersect output inference type.
*/
export type IntersectOutput<
TIntersectOptions extends IntersectOptions | IntersectOptionsAsync
Expand Down
189 changes: 188 additions & 1 deletion website/src/routes/api/(schemas)/intersect/index.mdx
Original file line number Diff line number Diff line change
@@ -1,10 +1,197 @@
---
title: intersect
description: Creates an intersect schema.
source: /schemas/intersect/intersect.ts
contributors:
- fabian-hiller
---

import { Link } from '@builder.io/qwik-city';
import { ApiList, Property } from '~/components';
import { properties } from './properties';

# intersect

> The content of this page is not yet ready. Until then, please use the [source code](https://github.com/fabian-hiller/valibot/blob/main/library/src/schemas/intersect/intersect.ts) or take a look at [issue #287](https://github.com/fabian-hiller/valibot/issues/287) to help us extend the API reference.
Creates an intersect schema.

> I recommend that you read the <Link href="/guides/intersections">intersections guide</Link> before using this schema function.
```ts
// Intersect schema with an optional pipe
const Schema = intersect<TOptions>(options, pipe);

// Intersect schema with an optional message and pipe
const Schema = intersect<TOptions>(options, message, pipe);
```

## Generics

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

## Parameters

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

### Explanation

With `intersect` you can validate if the input matches each of the given `options`. With `pipe` you can transform and validate the further details of the intersection. If the output of the intersection cannot be successfully merged, you can use `message` to customize the error message.

## Returns

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

## Examples

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

### Object intersection

Schema that combines two object schemas.

```ts
const ObjectSchema = intersect([
object({ foo: string() }),
object({ bar: number() }),
]);
```

## Related

The following APIs can be combined with `intersect`.

### Schemas

<ApiList
items={[
'any',
'array',
'bigint',
'blob',
'boolean',
'date',
'enum_',
'instance',
'literal',
'map',
'nan',
'never',
'nonNullable',
'nonNullish',
'nonOptional',
'null_',
'nullable',
'nullish',
'number',
'object',
'optional',
'picklist',
'record',
'recursive',
'set',
'special',
'string',
'symbol',
'tuple',
'undefined_',
'union',
'intersect',
'unknown',
'variant',
'void_',
]}
/>

### Methods

<ApiList
items={[
'brand',
'coerce',
'fallback',
'getDefault',
'getDefaults',
'getFallback',
'getFallbacks',
'is',
'parse',
'safeParse',
'transform',
]}
/>

### Transformations

<ApiList
items={[
'toCustom',
'toLowerCase',
'toMaxValue',
'toMinValue',
'toTrimmed',
'toTrimmedEnd',
'toTrimmedStart',
'toUpperCase',
]}
/>

### Validations

<ApiList
items={[
'bic',
'bytes',
'creditCard',
'cuid2',
'custom',
'decimal',
'email',
'emoji',
'endsWith',
'excludes',
'finite',
'hash',
'hexadecimal',
'hexColor',
'imei',
'includes',
'integer',
'ip',
'ipv4',
'ipv6',
'isoDate',
'isoDateTime',
'isoTime',
'isoTimeSecond',
'isoTimestamp',
'isoWeek',
'length',
'mac',
'mac48',
'mac64',
'maxBytes',
'maxLength',
'maxSize',
'maxValue',
'mimeType',
'minBytes',
'minLength',
'minSize',
'minValue',
'multipleOf',
'notBytes',
'notLength',
'notSize',
'notValue',
'octal',
'regex',
'safeInteger',
'size',
'startsWith',
'ulid',
'url',
'uuid',
'value',
]}
/>
68 changes: 68 additions & 0 deletions website/src/routes/api/(schemas)/intersect/properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { PropertyProps } from '~/components';

export const properties: Record<string, PropertyProps> = {
TOptions: {
type: {
type: 'custom',
name: 'IntersectOptions',
href: '../IntersectOptions/',
},
},
options: {
type: {
type: 'custom',
name: 'TOptions',
},
},
message: {
type: {
type: 'union',
options: [
{
type: 'custom',
name: 'ErrorMessage',
href: '../ErrorMessage/',
},
'undefined',
],
},
default: {
type: 'string',
value: 'Invalid type',
},
},
pipe: {
type: {
type: 'union',
options: [
{
type: 'custom',
name: 'Pipe',
href: '../Pipe/',
generics: [
{
type: 'custom',
name: 'IntersectOutput',
href: '../IntersectOutput/',
generics: [
{
type: 'custom',
name: 'TOptions',
href: '../TOptions/',
},
],
},
],
},
'undefined',
],
},
},
Schema: {
type: {
type: 'custom',
name: 'IntersectSchema',
href: '../IntersectSchema/',
},
},
};
3 changes: 3 additions & 0 deletions website/src/routes/api/(schemas)/union/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ contributors:
- thundermiracle
---

import { Link } from '@builder.io/qwik-city';
import { ApiList, Property } from '~/components';
import { properties } from './properties';

# union

Creates an union schema.

> I recommend that you read the <Link href="/guides/unions">unions guide</Link> before using this schema function.
```ts
// Union schema with an optional pipe
const Schema = union<TOptions>(options, pipe);
Expand Down
21 changes: 21 additions & 0 deletions website/src/routes/api/(types)/IntersectInput/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: IntersectInput
description: Intersect input inference type.
contributors:
- fabian-hiller
---

# IntersectInput

Intersect input inference type.

```ts
// Create object schemas
const ObjectSchemas = [
object({ key1: transform(string(), (input) => input.length) }),
object({ key2: transform(string(), (input) => input.length) }),
];

// Infer object intersect input type
type ObjectInput = IntersectInput<typeof ObjectSchemas>; // { key1: string } & { key2: string }
```
17 changes: 17 additions & 0 deletions website/src/routes/api/(types)/IntersectOptions/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: IntersectOptions
description: Intersect options type.
contributors:
- fabian-hiller
---

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

# IntersectOptions

Intersect options type.

## Definition

- `IntersectOptions` <Property {...properties.IntersectOptions} />
37 changes: 37 additions & 0 deletions website/src/routes/api/(types)/IntersectOptions/properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { PropertyProps } from '~/components';

export const properties: Record<string, PropertyProps> = {
IntersectOptions: {
type: {
type: 'custom',
name: 'MaybeReadonly',
href: '../MaybeReadonly/',
generics: [
{
type: 'tuple',
items: [
{
type: 'custom',
name: 'BaseSchema',
href: '../BaseSchema/',
},
{
type: 'custom',
name: 'BaseSchema',
href: '../BaseSchema/',
},
{
type: 'array',
spread: true,
item: {
type: 'custom',
name: 'BaseSchema',
href: '../BaseSchema/',
},
},
],
},
],
},
},
};
Loading

0 comments on commit c0831b0

Please sign in to comment.