diff --git a/.changeset/brown-bags-matter.md b/.changeset/brown-bags-matter.md new file mode 100644 index 000000000..e4d8cd3fd --- /dev/null +++ b/.changeset/brown-bags-matter.md @@ -0,0 +1,5 @@ +--- +"@hey-api/openapi-ts": patch +--- + +fix(valibot): handle `time` string format diff --git a/packages/openapi-ts-tests/main/test/3.1.x.test.ts b/packages/openapi-ts-tests/main/test/3.1.x.test.ts index 7b7d94765..84e20de05 100644 --- a/packages/openapi-ts-tests/main/test/3.1.x.test.ts +++ b/packages/openapi-ts-tests/main/test/3.1.x.test.ts @@ -994,6 +994,14 @@ describe(`OpenAPI ${version}`, () => { }), description: 'anyOf string and binary string', }, + { + config: createConfig({ + input: 'time-format.yaml', + output: 'time-format', + plugins: ['valibot'], + }), + description: 'generates correct valibot schema for time format', + }, ]; it.each(scenarios)('$description', async ({ config }) => { diff --git a/packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/time-format/valibot.gen.ts b/packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/time-format/valibot.gen.ts new file mode 100644 index 000000000..06301e735 --- /dev/null +++ b/packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/time-format/valibot.gen.ts @@ -0,0 +1,20 @@ +// This file is auto-generated by @hey-api/openapi-ts + +import * as v from 'valibot'; + +export const vGetSearchData = v.object({ + body: v.optional(v.never()), + path: v.optional(v.never()), + query: v.object({ + start_time: v.optional(v.pipe(v.string(), v.isoTimeSecond())), + end_time: v.pipe(v.string(), v.isoTimeSecond()) + }) +}); + +/** + * Success + */ +export const vGetSearchResponse = v.object({ + result: v.optional(v.string()), + scheduled_time: v.optional(v.pipe(v.string(), v.isoTimeSecond())) +}); diff --git a/packages/openapi-ts/src/plugins/valibot/v1/toAst/string.ts b/packages/openapi-ts/src/plugins/valibot/v1/toAst/string.ts index cc7e46edb..bdd4493de 100644 --- a/packages/openapi-ts/src/plugins/valibot/v1/toAst/string.ts +++ b/packages/openapi-ts/src/plugins/valibot/v1/toAst/string.ts @@ -72,6 +72,16 @@ export const stringToAst = ({ }), ); break; + case 'time': + pipes.push( + tsc.callExpression({ + functionName: tsc.propertyAccessExpression({ + expression: v.placeholder, + name: identifiers.actions.isoTimeSecond, + }), + }), + ); + break; case 'uri': pipes.push( tsc.callExpression({ @@ -83,7 +93,6 @@ export const stringToAst = ({ ); break; case 'email': - case 'time': case 'uuid': pipes.push( tsc.callExpression({ diff --git a/specs/3.1.x/time-format.yaml b/specs/3.1.x/time-format.yaml new file mode 100644 index 000000000..17ad9307b --- /dev/null +++ b/specs/3.1.x/time-format.yaml @@ -0,0 +1,35 @@ +openapi: 3.1.0 +info: + title: OpenAPI 3.1.0 time format example + version: '1' +paths: + /search: + get: + summary: Search with time parameter + parameters: + - name: start_time + in: query + description: Start time in HH:MM:SS format + schema: + type: string + format: time + - name: end_time + in: query + description: End time in HH:MM:SS format + required: true + schema: + type: string + format: time + responses: + '200': + description: Success + content: + application/json: + schema: + type: object + properties: + result: + type: string + scheduled_time: + type: string + format: time