diff --git a/library/src/schemas/intersect/types.ts b/library/src/schemas/intersect/types.ts index 063802e98..62ca60abf 100644 --- a/library/src/schemas/intersect/types.ts +++ b/library/src/schemas/intersect/types.ts @@ -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 @@ -21,7 +21,7 @@ export type IntersectInput< : never; /** - * Intersect output type. + * Intersect output inference type. */ export type IntersectOutput< TIntersectOptions extends IntersectOptions | IntersectOptionsAsync diff --git a/website/src/routes/api/(schemas)/intersect/index.mdx b/website/src/routes/api/(schemas)/intersect/index.mdx index 078d67055..3dbfe7a56 100644 --- a/website/src/routes/api/(schemas)/intersect/index.mdx +++ b/website/src/routes/api/(schemas)/intersect/index.mdx @@ -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 intersections guide before using this schema function. + +```ts +// Intersect schema with an optional pipe +const Schema = intersect(options, pipe); + +// Intersect schema with an optional message and pipe +const Schema = intersect(options, message, pipe); +``` + +## Generics + +- `TOptions` + +## Parameters + +- `options` +- `message` +- `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` + +## 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 + + + +### Methods + + + +### Transformations + + + +### Validations + + diff --git a/website/src/routes/api/(schemas)/intersect/properties.ts b/website/src/routes/api/(schemas)/intersect/properties.ts new file mode 100644 index 000000000..71e222e04 --- /dev/null +++ b/website/src/routes/api/(schemas)/intersect/properties.ts @@ -0,0 +1,68 @@ +import type { PropertyProps } from '~/components'; + +export const properties: Record = { + 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/', + }, + }, +}; diff --git a/website/src/routes/api/(schemas)/union/index.mdx b/website/src/routes/api/(schemas)/union/index.mdx index 115c9b796..9d93cdf14 100644 --- a/website/src/routes/api/(schemas)/union/index.mdx +++ b/website/src/routes/api/(schemas)/union/index.mdx @@ -7,6 +7,7 @@ contributors: - thundermiracle --- +import { Link } from '@builder.io/qwik-city'; import { ApiList, Property } from '~/components'; import { properties } from './properties'; @@ -14,6 +15,8 @@ import { properties } from './properties'; Creates an union schema. +> I recommend that you read the unions guide before using this schema function. + ```ts // Union schema with an optional pipe const Schema = union(options, pipe); diff --git a/website/src/routes/api/(types)/IntersectInput/index.mdx b/website/src/routes/api/(types)/IntersectInput/index.mdx new file mode 100644 index 000000000..e376e7e00 --- /dev/null +++ b/website/src/routes/api/(types)/IntersectInput/index.mdx @@ -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; // { key1: string } & { key2: string } +``` diff --git a/website/src/routes/api/(types)/IntersectOptions/index.mdx b/website/src/routes/api/(types)/IntersectOptions/index.mdx new file mode 100644 index 000000000..a21560904 --- /dev/null +++ b/website/src/routes/api/(types)/IntersectOptions/index.mdx @@ -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` diff --git a/website/src/routes/api/(types)/IntersectOptions/properties.ts b/website/src/routes/api/(types)/IntersectOptions/properties.ts new file mode 100644 index 000000000..9af8bf293 --- /dev/null +++ b/website/src/routes/api/(types)/IntersectOptions/properties.ts @@ -0,0 +1,37 @@ +import type { PropertyProps } from '~/components'; + +export const properties: Record = { + 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/', + }, + }, + ], + }, + ], + }, + }, +}; diff --git a/website/src/routes/api/(types)/IntersectOutput/index.mdx b/website/src/routes/api/(types)/IntersectOutput/index.mdx new file mode 100644 index 000000000..274901471 --- /dev/null +++ b/website/src/routes/api/(types)/IntersectOutput/index.mdx @@ -0,0 +1,21 @@ +--- +title: IntersectOutput +description: Intersect output inference type. +contributors: + - fabian-hiller +--- + +# IntersectOutput + +Intersect output 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 output type +type ObjectOutput = ObjectOutput; // { key1: number } & { key2: number } +``` diff --git a/website/src/routes/api/(types)/IntersectSchema/index.mdx b/website/src/routes/api/(types)/IntersectSchema/index.mdx new file mode 100644 index 000000000..0c45f4b4e --- /dev/null +++ b/website/src/routes/api/(types)/IntersectSchema/index.mdx @@ -0,0 +1,21 @@ +--- +title: IntersectSchema +description: Intersect schema type. +contributors: + - fabian-hiller +--- + +import { Property } from '~/components'; +import { properties } from './properties'; + +# IntersectSchema + +Intersect schema type. + +## Definition + +- `IntersectSchema` + - `type` + - `options` + - `message` + - `pipe` diff --git a/website/src/routes/api/(types)/IntersectSchema/properties.ts b/website/src/routes/api/(types)/IntersectSchema/properties.ts new file mode 100644 index 000000000..59a76ef97 --- /dev/null +++ b/website/src/routes/api/(types)/IntersectSchema/properties.ts @@ -0,0 +1,87 @@ +import type { PropertyProps } from '~/components'; + +export const properties: Record = { + BaseSchema: { + type: { + type: 'custom', + name: 'BaseSchema', + href: '../BaseSchema/', + generics: [ + { + type: 'custom', + name: 'IntersectInput', + href: '../IntersectInput/', + generics: [ + { + type: 'custom', + name: 'TOptions', + href: '../TOptions/', + }, + ], + }, + { + type: 'custom', + name: 'TOutput', + default: { + type: 'custom', + name: 'IntersectOutput', + href: '../IntersectOutput/', + generics: [ + { + type: 'custom', + name: 'TOptions', + href: '../TOptions/', + }, + ], + }, + }, + ], + }, + }, + type: { + type: { + type: 'string', + value: 'intersect', + }, + }, + options: { + type: { + type: 'custom', + name: 'TOptions', + }, + }, + message: { + type: { + type: 'custom', + name: 'ErrorMessage', + href: '../ErrorMessage/', + }, + }, + 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', + ], + }, + }, +}; diff --git a/website/src/routes/api/menu.md b/website/src/routes/api/menu.md index 50b3538de..26b31a04b 100644 --- a/website/src/routes/api/menu.md +++ b/website/src/routes/api/menu.md @@ -207,6 +207,10 @@ - [FallbackInfo](/api/FallbackInfo/) - [Input](/api/Input/) - [InstanceSchema](/api/InstanceSchema/) +- [IntersectInput](/api/IntersectInput/) +- [IntersectOptions](/api/IntersectOptions/) +- [IntersectOutput](/api/IntersectOutput/) +- [IntersectSchema](/api/IntersectSchema/) - [InvalidActionResult](/api/InvalidActionResult/) - [Issue](/api/Issue/) - [IssueOrigin](/api/IssueOrigin/) diff --git a/website/src/routes/guides/(main-concepts)/schemas/index.mdx b/website/src/routes/guides/(main-concepts)/schemas/index.mdx index 0ba87f56a..f1d1681ec 100644 --- a/website/src/routes/guides/(main-concepts)/schemas/index.mdx +++ b/website/src/routes/guides/(main-concepts)/schemas/index.mdx @@ -181,6 +181,6 @@ const UnknownSchema = v.unknown(); // unknown const VariantSchema = v.variant('type', [ v.object({ type: v.literal('a'), foo: v.string() }), v.object({ type: v.literal('b'), bar: v.number() }), -]); // { type: 'a', foo: string } | { type: 'b', bar: number } +]); // { type: 'a'; foo: string } | { type: 'b'; bar: number } const VoidSchema = v.void_(); // void ``` diff --git a/website/src/routes/guides/(schemas)/intersections/index.mdx b/website/src/routes/guides/(schemas)/intersections/index.mdx index 5c387ca29..d120a515d 100644 --- a/website/src/routes/guides/(schemas)/intersections/index.mdx +++ b/website/src/routes/guides/(schemas)/intersections/index.mdx @@ -42,7 +42,7 @@ 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; } +]); // { 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. diff --git a/website/src/routes/guides/(schemas)/objects/index.mdx b/website/src/routes/guides/(schemas)/objects/index.mdx index 74d725c78..7ccb3cb7a 100644 --- a/website/src/routes/guides/(schemas)/objects/index.mdx +++ b/website/src/routes/guides/(schemas)/objects/index.mdx @@ -108,7 +108,7 @@ Instead of `string`, you can also use