Skip to content

Commit c5d45fc

Browse files
authored
feat(zod): add support for string format time (#2063)
1 parent 0c6fd5a commit c5d45fc

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

packages/zod/src/compatibleV4.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, it, expect } from 'vitest';
22
import {
33
isZodVersionV4,
44
getZodDateFormat,
5+
getZodTimeFormat,
56
getZodDateTimeFormat,
67
} from './compatibleV4';
78

@@ -67,6 +68,16 @@ describe('getZodDateFormat', () => {
6768
});
6869
});
6970

71+
describe('getZodTimeFormat', () => {
72+
it('should return "iso.time" when isZodV4 is true', () => {
73+
expect(getZodTimeFormat(true)).toBe('iso.time');
74+
});
75+
76+
it('should return "time" when isZodV4 is false', () => {
77+
expect(getZodTimeFormat(false)).toBe('time');
78+
});
79+
});
80+
7081
describe('getZodDateTimeFormat', () => {
7182
it('should return "iso.datetime" when isZodV4 is true', () => {
7283
expect(getZodDateTimeFormat(true)).toBe('iso.datetime');

packages/zod/src/compatibleV4.ts

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export const getZodDateFormat = (isZodV4: boolean) => {
2424
return isZodV4 ? 'iso.date' : 'date';
2525
};
2626

27+
export const getZodTimeFormat = (isZodV4: boolean) => {
28+
return isZodV4 ? 'iso.time' : 'time';
29+
};
30+
2731
export const getZodDateTimeFormat = (isZodV4: boolean) => {
2832
return isZodV4 ? 'iso.datetime' : 'datetime';
2933
};

packages/zod/src/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
import {
2424
isZodVersionV4,
2525
getZodDateFormat,
26+
getZodTimeFormat,
2627
getZodDateTimeFormat,
2728
} from './compatibleV4';
2829
import uniq from 'lodash.uniq';
@@ -309,6 +310,13 @@ export const generateZodValidationSchemaDefinition = (
309310
break;
310311
}
311312

313+
if (schema.format === 'time') {
314+
const formatAPI = getZodTimeFormat(isZodV4);
315+
316+
functions.push([formatAPI, undefined]);
317+
break;
318+
}
319+
312320
if (schema.format === 'date-time') {
313321
const options = context.output.override.zod?.dateTimeOptions;
314322
const formatAPI = getZodDateTimeFormat(isZodV4);

0 commit comments

Comments
 (0)