Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/stale-sloths-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix(zod): add `dates.local` option to allow unqualified (timezone-less) datetimes
3 changes: 3 additions & 0 deletions examples/openapi-ts-angular-common/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,8 @@
}
}
}
},
"cli": {
"analytics": false
}
}
3 changes: 3 additions & 0 deletions examples/openapi-ts-angular/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,8 @@
}
}
}
},
"cli": {
"analytics": false
}
}
1 change: 1 addition & 0 deletions packages/openapi-ts/src/plugins/zod/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const defaultConfig: ZodPlugin['Config'] = {

plugin.config.dates = context.valueToObject({
defaultValue: {
local: false,
offset: false,
},
value: plugin.config.dates,
Expand Down
29 changes: 17 additions & 12 deletions packages/openapi-ts/src/plugins/zod/mini/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,15 @@ const stringTypeToZodSchema = ({
}),
});

const dateTimeOptions: { key: string; value: boolean }[] = [];

if (plugin.config.dates.offset) {
dateTimeOptions.push({ key: 'offset', value: true });
}
if (plugin.config.dates.local) {
dateTimeOptions.push({ key: 'local', value: true });
}

if (schema.format) {
switch (schema.format) {
case 'date':
Expand All @@ -558,18 +567,14 @@ const stringTypeToZodSchema = ({
}),
name: identifiers.datetime,
}),
parameters: plugin.config.dates.offset
? [
tsc.objectExpression({
obj: [
{
key: 'offset',
value: true,
},
],
}),
]
: [],
parameters:
dateTimeOptions.length > 0
? [
tsc.objectExpression({
obj: dateTimeOptions,
}),
]
: [],
});
break;
case 'email':
Expand Down
18 changes: 18 additions & 0 deletions packages/openapi-ts/src/plugins/zod/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ export type UserConfig = Plugin.Name<'zod'> & {
* date validation features.
*/
dates?: {
/**
* Whether to allow unqualified (timezone-less) datetimes:
*
* When enabled, Zod will accept datetime strings without timezone information.
* When disabled, Zod will require timezone information in datetime strings.
*
* @default false
*/
local?: boolean;
/**
* Whether to include timezone offset information when handling dates.
*
Expand Down Expand Up @@ -365,6 +374,15 @@ export type Config = Plugin.Name<'zod'> & {
* date validation features.
*/
dates: {
/**
* Whether to allow unqualified (timezone-less) datetimes:
*
* When enabled, Zod will accept datetime strings without timezone information.
* When disabled, Zod will require timezone information in datetime strings.
*
* @default false
*/
local: boolean;
/**
* Whether to include timezone offset information when handling dates.
*
Expand Down
29 changes: 17 additions & 12 deletions packages/openapi-ts/src/plugins/zod/v3/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,15 @@ const stringTypeToZodSchema = ({
}),
});

const dateTimeOptions: { key: string; value: boolean }[] = [];

if (plugin.config.dates.offset) {
dateTimeOptions.push({ key: 'offset', value: true });
}
if (plugin.config.dates.local) {
dateTimeOptions.push({ key: 'local', value: true });
}

if (schema.format) {
switch (schema.format) {
case 'date':
Expand All @@ -465,18 +474,14 @@ const stringTypeToZodSchema = ({
expression: stringExpression,
name: identifiers.datetime,
}),
parameters: plugin.config.dates.offset
? [
tsc.objectExpression({
obj: [
{
key: 'offset',
value: true,
},
],
}),
]
: [],
parameters:
dateTimeOptions.length > 0
? [
tsc.objectExpression({
obj: dateTimeOptions,
}),
]
: [],
});
break;
case 'email':
Expand Down
29 changes: 17 additions & 12 deletions packages/openapi-ts/src/plugins/zod/v4/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,15 @@ const stringTypeToZodSchema = ({
}),
});

const dateTimeOptions: { key: string; value: boolean }[] = [];

if (plugin.config.dates.offset) {
dateTimeOptions.push({ key: 'offset', value: true });
}
if (plugin.config.dates.local) {
dateTimeOptions.push({ key: 'local', value: true });
}

if (schema.format) {
switch (schema.format) {
case 'date':
Expand All @@ -537,18 +546,14 @@ const stringTypeToZodSchema = ({
}),
name: identifiers.datetime,
}),
parameters: plugin.config.dates.offset
? [
tsc.objectExpression({
obj: [
{
key: 'offset',
value: true,
},
],
}),
]
: [],
parameters:
dateTimeOptions.length > 0
? [
tsc.objectExpression({
obj: dateTimeOptions,
}),
]
: [],
});
break;
case 'email':
Expand Down
Loading