Skip to content

feat(valibot): single schema per request #2226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .changeset/dry-suits-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
'@hey-api/openapi-ts': minor
---

feat(valibot): generate a single schema for requests

### Single Valibot schema per request

Previously, we generated a separate schema for each endpoint parameter and request body. In v0.76.0, a single request schema is generated for the whole endpoint. It may contain a request body, parameters, and headers.

```ts
const vData = v.object({
body: v.optional(v.object({
foo: v.optional(v.string()),
bar: v.optional(v.union([v.number(), v.null()])),
})),
headers: v.optional(v.never()),
path: v.object({
baz: v.string(),
}),
query: v.optional(v.never()),
});
```

If you need to access individual fields, you can do so using the [`.entries`](https://valibot.dev/api/object/) API. For example, we can get the request body schema with `vData.entries.body`.
5 changes: 5 additions & 0 deletions .changeset/six-birds-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix(valibot): add `metadata` option to generate additional metadata for documentation, code generation, AI structured outputs, form validation, and other purposes
24 changes: 24 additions & 0 deletions docs/openapi-ts/migrating.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ This config option is deprecated and will be removed in favor of [clients](./cli

This config option is deprecated and will be removed.

## v0.76.0

### Single Valibot schema per request

Previously, we generated a separate schema for each endpoint parameter and request body. In v0.76.0, a single request schema is generated for the whole endpoint. It may contain a request body, parameters, and headers.

```ts
const vData = v.object({
body: v.optional(
v.object({
foo: v.optional(v.string()),
bar: v.optional(v.union([v.number(), v.null()])),
}),
),
headers: v.optional(v.never()),
path: v.object({
baz: v.string(),
}),
query: v.optional(v.never()),
});
```

If you need to access individual fields, you can do so using the [`.entries`](https://valibot.dev/api/object/) API. For example, we can get the request body schema with `vData.entries.body`.

## v0.75.0

### Updated TanStack Query options
Expand Down
79 changes: 51 additions & 28 deletions docs/openapi-ts/plugins/valibot.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@ Launch demo
## Features

- seamless integration with `@hey-api/openapi-ts` ecosystem
- Valibot schemas for request payloads, parameters, and responses
- Valibot schemas for requests, responses, and reusable definitions

## Installation

In your [configuration](/openapi-ts/get-started), add `valibot` to your plugins and you'll be ready to generate Valibot artifacts. :tada:

```js
import { defaultPlugins } from '@hey-api/openapi-ts';

export default {
input: 'https://get.heyapi.dev/hey-api/backend',
output: 'src/client',
plugins: [
...defaultPlugins,
// ...other plugins
'valibot', // [!code ++]
],
};
Expand All @@ -50,13 +48,11 @@ export default {
To automatically validate response data in your SDKs, set `sdk.validator` to `true`.

```js
import { defaultPlugins } from '@hey-api/openapi-ts';

export default {
input: 'https://get.heyapi.dev/hey-api/backend',
output: 'src/client',
plugins: [
...defaultPlugins,
// ...other plugins
'valibot',
{
name: '@hey-api/sdk', // [!code ++]
Expand All @@ -70,6 +66,32 @@ export default {

The Valibot plugin will generate the following artifacts, depending on the input specification.

## Requests

A single request schema is generated for each endpoint. It may contain a request body, parameters, and headers.

```ts
const vData = v.object({
body: v.optional(
v.object({
foo: v.optional(v.string()),
bar: v.optional(v.union([v.number(), v.null()])),
}),
),
headers: v.optional(v.never()),
path: v.object({
baz: v.string(),
}),
query: v.optional(v.never()),
});
```

::: tip
If you need to access individual fields, you can do so using the [`.entries`](https://valibot.dev/api/object/) API. For example, we can get the request body schema with `vData.entries.body`.
:::

You can customize the naming and casing pattern for requests using the `requests.name` and `requests.case` options.

## Responses

A single Valibot schema is generated for all endpoint's responses. If the endpoint describes multiple responses, the generated schema is a union of all possible response shapes.
Expand All @@ -85,37 +107,38 @@ const vResponse = v.union([
]);
```

## Request Bodies

If an endpoint describes a request body, we will generate a Valibot schema representing its shape.
You can customize the naming and casing pattern for responses using the `responses.name` and `responses.case` options.

```ts
const vData = v.object({
foo: v.optional(v.string()),
bar: v.optional(v.union([v.number(), v.null()])),
});
```
## Definitions

## Parameters

A separate Valibot schema is generated for every request parameter.
A Valibot schema is generated for every reusable definition from your input.

```ts
const vParameterFoo = v.pipe(v.number(), v.integer());
const vFoo = v.pipe(v.number(), v.integer());

const vParameterBar = v.string();
const vBar = v.object({
bar: v.optional(v.array(v.pipe(v.number(), v.integer()))),
});
```

## Schemas
You can customize the naming and casing pattern for definitions using the `definitions.name` and `definitions.case` options.

A separate Valibot schema is generated for every reusable schema.
## Metadata

```ts
const vFoo = v.pipe(v.number(), v.integer());
It's often useful to associate a schema with some additional [metadata](https://valibot.dev/api/metadata/) for documentation, code generation, AI structured outputs, form validation, and other purposes. If this is your use case, you can set `metadata` to `true` to generate additional metadata about schemas.

const vBar = v.object({
bar: v.optional(v.union([v.array(v.unknown()), v.null()])),
});
```js
export default {
input: 'https://get.heyapi.dev/hey-api/backend',
output: 'src/client',
plugins: [
// ...other plugins
{
metadata: true, // [!code ++]
name: 'valibot',
},
],
};
```

<!--@include: ../../examples.md-->
Expand Down
5 changes: 4 additions & 1 deletion packages/openapi-ts-tests/test/3.1.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,10 @@ describe(`OpenAPI ${version}`, () => {
input: 'validators.yaml',
output: 'validators-metadata',
plugins: [
'valibot',
{
metadata: true,
name: 'valibot',
},
{
metadata: true,
name: 'zod',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ export const vBar = v.object({
foo: v.pipe(v.number(), v.integer())
});

export const vPostFooData = v.object({
body: v.optional(v.never()),
headers: v.optional(v.never()),
path: v.optional(v.never()),
query: v.optional(v.never())
});

/**
* OK
*/
Expand Down
Loading