Skip to content

Commit 6f55225

Browse files
authored
Merge pull request #2060 from hey-api/feat/plugin-valibot
fix(valibot): add valibot plugin
2 parents 75af86e + 6adc3ef commit 6f55225

File tree

77 files changed

+4473
-132
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+4473
-132
lines changed

.changeset/three-lobsters-grow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hey-api/openapi-ts': patch
3+
---
4+
5+
fix(valibot): add valibot plugin

docs/.vitepress/config/en.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ export default defineConfig({
9494
{
9595
collapsed: true,
9696
items: [
97+
{
98+
link: '/openapi-ts/plugins/valibot',
99+
text: 'Valibot',
100+
},
97101
{
98102
link: '/openapi-ts/plugins/zod',
99103
text: 'Zod',
@@ -118,10 +122,6 @@ export default defineConfig({
118122
link: '/openapi-ts/plugins/typebox',
119123
text: 'TypeBox <span data-soon>soon</span>',
120124
},
121-
{
122-
link: '/openapi-ts/plugins/valibot',
123-
text: 'Valibot <span data-soon>soon</span>',
124-
},
125125
{
126126
link: '/openapi-ts/plugins/yup',
127127
text: 'Yup <span data-soon>soon</span>',

docs/openapi-ts/plugins.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ These plugins help reduce boilerplate associated with third-party dependencies.
2626
- [`@tanstack/svelte-query`](/openapi-ts/plugins/tanstack-query)
2727
- [`@tanstack/vue-query`](/openapi-ts/plugins/tanstack-query)
2828
- [`fastify`](/openapi-ts/plugins/fastify)
29+
- [`valibot`](/openapi-ts/plugins/valibot)
2930
- [`zod`](/openapi-ts/plugins/zod)
3031

3132
## Upcoming
@@ -47,7 +48,6 @@ The following plugins are planned but not in development yet. You can help us pr
4748
- [Supertest](/openapi-ts/plugins/supertest) <span data-soon>Soon</span>
4849
- [SWR](/openapi-ts/plugins/swr) <span data-soon>Soon</span>
4950
- [TypeBox](/openapi-ts/plugins/typebox) <span data-soon>Soon</span>
50-
- [Valibot](/openapi-ts/plugins/valibot) <span data-soon>Soon</span>
5151
- [Yup](/openapi-ts/plugins/yup) <span data-soon>Soon</span>
5252
- [Zustand](/openapi-ts/plugins/zustand) <span data-soon>Soon</span>
5353

docs/openapi-ts/plugins/valibot.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,74 @@ title: Valibot
33
description: Valibot plugin for Hey API. Compatible with all our features.
44
---
55

6-
# Valibot <span data-soon>soon</span>
6+
# Valibot
77

88
::: warning
9-
This feature isn't in development yet. Help us prioritize it by voting on [GitHub](https://github.com/hey-api/openapi-ts/issues/1474).
9+
This feature is in development! :tada: Try it out and provide feedback on [GitHub](https://github.com/hey-api/openapi-ts/issues/1474).
1010
:::
1111

1212
### About
1313

1414
[Valibot](https://valibot.dev) is the open source schema library for TypeScript with bundle size, type safety and developer experience in mind.
1515

16+
<!-- ### Demo
17+
18+
<button class="buttonLink" @click="(event) => embedProject('hey-api-client-fetch-plugin-valibot-example')(event)">
19+
Launch demo
20+
</button> -->
21+
22+
## Features
23+
24+
- seamless integration with `@hey-api/openapi-ts` ecosystem
25+
- Valibot schemas for requests, responses, and reusable components
26+
27+
## Installation
28+
29+
In your [configuration](/openapi-ts/get-started), add `valibot` to your plugins and you'll be ready to generate Valibot artifacts. :tada:
30+
31+
```js
32+
import { defaultPlugins } from '@hey-api/openapi-ts';
33+
34+
export default {
35+
input: 'https://get.heyapi.dev/hey-api/backend',
36+
output: 'src/client',
37+
plugins: [
38+
...defaultPlugins,
39+
'@hey-api/client-fetch',
40+
'valibot', // [!code ++]
41+
],
42+
};
43+
```
44+
45+
## SDKs
46+
47+
To automatically validate response data in your SDKs, set `sdk.validator` to `true`.
48+
49+
```js
50+
import { defaultPlugins } from '@hey-api/openapi-ts';
51+
52+
export default {
53+
input: 'https://get.heyapi.dev/hey-api/backend',
54+
output: 'src/client',
55+
plugins: [
56+
...defaultPlugins,
57+
'@hey-api/client-fetch',
58+
'valibot',
59+
{
60+
name: '@hey-api/sdk', // [!code ++]
61+
validator: true, // [!code ++]
62+
},
63+
],
64+
};
65+
```
66+
67+
## Output
68+
69+
The Valibot plugin will generate the following artifacts, depending on the input specification.
70+
71+
## Schemas
72+
73+
More information will be provided as we finalize the plugin.
74+
75+
<!--@include: ../../examples.md-->
1676
<!--@include: ../../sponsors.md-->

docs/openapi-ts/validators.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ Whatever your reason to use validators might be, you can rest assured that you'r
1313

1414
Hey API natively supports the following validators.
1515

16+
- [Valibot](/openapi-ts/plugins/valibot)
1617
- [Zod](/openapi-ts/plugins/zod)
1718
- [Ajv](/openapi-ts/plugins/ajv) <span data-soon>Soon</span>
1819
- [Arktype](/openapi-ts/plugins/arktype) <span data-soon>Soon</span>
1920
- [Joi](/openapi-ts/plugins/joi) <span data-soon>Soon</span>
2021
- [TypeBox](/openapi-ts/plugins/typebox) <span data-soon>Soon</span>
21-
- [Valibot](/openapi-ts/plugins/valibot) <span data-soon>Soon</span>
2222
- [Yup](/openapi-ts/plugins/yup) <span data-soon>Soon</span>
2323

2424
Don't see your validator? Let us know your interest by [opening an issue](https://github.com/hey-api/openapi-ts/issues).

packages/openapi-ts-tests/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"ts-node": "10.9.2",
5959
"tslib": "2.8.1",
6060
"typescript": "5.8.3",
61+
"valibot": "1.1.0",
6162
"vue": "3.5.13",
6263
"zod": "3.23.8"
6364
}

packages/openapi-ts-tests/test/3.0.x.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ describe(`OpenAPI ${version}`, () => {
419419
config: createConfig({
420420
input: 'enum-null.json',
421421
output: 'enum-null',
422-
plugins: ['@hey-api/typescript', 'zod'],
422+
plugins: ['@hey-api/typescript', 'valibot', 'zod'],
423423
}),
424424
description: 'handles null enums',
425425
},
@@ -586,9 +586,9 @@ describe(`OpenAPI ${version}`, () => {
586586
config: createConfig({
587587
input: 'validators.json',
588588
output: 'validators',
589-
plugins: ['zod'],
589+
plugins: ['valibot', 'zod'],
590590
}),
591-
description: 'generates Zod schemas',
591+
description: 'generates validator schemas',
592592
},
593593
];
594594

packages/openapi-ts-tests/test/3.1.x.test.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ describe(`OpenAPI ${version}`, () => {
433433
config: createConfig({
434434
input: 'enum-null.json',
435435
output: 'enum-null',
436-
plugins: ['@hey-api/typescript', 'zod'],
436+
plugins: ['@hey-api/typescript', 'valibot', 'zod'],
437437
}),
438438
description: 'handles null enums',
439439
},
@@ -567,7 +567,7 @@ describe(`OpenAPI ${version}`, () => {
567567
config: createConfig({
568568
input: 'schema-const.yaml',
569569
output: 'schema-const',
570-
plugins: ['@hey-api/typescript', 'zod'],
570+
plugins: ['@hey-api/typescript', 'valibot', 'zod'],
571571
}),
572572
description: 'handles various constants',
573573
},
@@ -684,41 +684,41 @@ describe(`OpenAPI ${version}`, () => {
684684
config: createConfig({
685685
input: 'validators.json',
686686
output: 'validators',
687-
plugins: ['zod'],
687+
plugins: ['valibot', 'zod'],
688688
}),
689-
description: 'generates Zod schemas',
689+
description: 'generates validator schemas',
690690
},
691691
{
692692
config: createConfig({
693-
input: 'zod-bigint-min-max.json',
694-
output: 'zod-bigint-min-max',
695-
plugins: ['zod'],
693+
input: 'validators-bigint-min-max.json',
694+
output: 'validators-bigint-min-max',
695+
plugins: ['valibot', 'zod'],
696696
}),
697-
description: 'Zod schemas with BigInt and min/max constraints',
697+
description: 'validator schemas with BigInt and min/max constraints',
698698
},
699699
{
700700
config: createConfig({
701-
input: 'zod-circular-ref.json',
702-
output: 'zod-circular-ref',
703-
plugins: ['zod'],
701+
input: 'validators-circular-ref.json',
702+
output: 'validators-circular-ref',
703+
plugins: ['valibot', 'zod'],
704704
}),
705-
description: 'Zod schemas with circular reference',
705+
description: 'validator schemas with circular reference',
706706
},
707707
{
708708
config: createConfig({
709-
input: 'zod-circular-ref-2.yaml',
710-
output: 'zod-circular-ref-2',
711-
plugins: ['zod'],
709+
input: 'validators-circular-ref-2.yaml',
710+
output: 'validators-circular-ref-2',
711+
plugins: ['valibot', 'zod'],
712712
}),
713-
description: 'Zod schemas with circular reference 2',
713+
description: 'validator schemas with circular reference 2',
714714
},
715715
{
716716
config: createConfig({
717-
input: 'zod-union-merge.json',
718-
output: 'zod-union-merge',
719-
plugins: ['zod'],
717+
input: 'validators-union-merge.json',
718+
output: 'validators-union-merge',
719+
plugins: ['valibot', 'zod'],
720720
}),
721-
description: "Zod schemas with merged unions (can't use .merge())",
721+
description: "validator schemas with merged unions (can't use .merge())",
722722
},
723723
];
724724

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
import type { Options as ClientOptions, TDataShape, Client } from '@hey-api/client-fetch';
4+
import type { PostFooData, PostFooResponse } from './types.gen';
5+
import { postFooResponseTransformer } from './transformers.gen';
6+
import { vPostFooResponse } from './valibot.gen';
7+
import * as v from 'valibot';
8+
import { client as _heyApiClient } from './client.gen';
9+
10+
export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean> = ClientOptions<TData, ThrowOnError> & {
11+
/**
12+
* You can provide a client instance returned by `createClient()` instead of
13+
* individual options. This might be also useful if you want to implement a
14+
* custom client.
15+
*/
16+
client?: Client;
17+
/**
18+
* You can pass arbitrary values through the `meta` object. This can be
19+
* used to access values that aren't defined as part of the SDK function.
20+
*/
21+
meta?: Record<string, unknown>;
22+
};
23+
24+
export const postFoo = <ThrowOnError extends boolean = false>(options?: Options<PostFooData, ThrowOnError>) => {
25+
return (options?.client ?? _heyApiClient).post<PostFooResponse, unknown, ThrowOnError>({
26+
responseTransformer: postFooResponseTransformer,
27+
responseValidator: async (data) => {
28+
return await v.parseAsync(vPostFooResponse, data);
29+
},
30+
url: '/foo',
31+
...options
32+
});
33+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
import * as v from 'valibot';
4+
5+
export const vFoo = v.object({
6+
bar: v.optional(v.pipe(v.number(), v.integer())),
7+
foo: v.bigint(),
8+
id: v.string()
9+
});
10+
11+
export const vBar = v.object({
12+
foo: v.pipe(v.number(), v.integer())
13+
});
14+
15+
export const vPostFooResponse = vFoo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export type Foo = {
4+
bar?: number;
5+
foo: bigint;
6+
id: string;
7+
};
8+
9+
export type Bar = {
10+
foo: number;
11+
[key: string]: number;
12+
};
13+
14+
export type PostFooData = {
15+
body?: never;
16+
path?: never;
17+
query?: never;
18+
url: '/foo';
19+
};
20+
21+
export type PostFooResponses = {
22+
/**
23+
* OK
24+
*/
25+
200: Foo;
26+
};
27+
28+
export type PostFooResponse = PostFooResponses[keyof PostFooResponses];
29+
30+
export type ClientOptions = {
31+
baseUrl: string;
32+
};

0 commit comments

Comments
 (0)