From 82fcd7bb00bd86775d6208d907bdf9db2abd78e3 Mon Sep 17 00:00:00 2001 From: Sam Chung Date: Sat, 20 Apr 2024 13:47:30 +1000 Subject: [PATCH] Support `.date()`, `.time()` and `.duration()` string formats (#252) --- package.json | 2 +- pnpm-lock.yaml | 8 ++++++-- src/create/schema/parsers/string.test.ts | 3 +++ src/create/schema/parsers/string.ts | 12 ++++++++++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 53559e0..ad4c15e 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "openapi3-ts": "4.3.1", "skuba": "8.0.0", "yaml": "2.4.1", - "zod": "3.22.4" + "zod": "3.23.0-beta.2" }, "peerDependencies": { "zod": "^3.21.4" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2b809d4..c76236c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -24,8 +24,8 @@ devDependencies: specifier: 2.4.1 version: 2.4.1 zod: - specifier: 3.22.4 - version: 3.22.4 + specifier: 3.23.0-beta.2 + version: 3.23.0-beta.2 packages: @@ -8721,3 +8721,7 @@ packages: /zod@3.22.4: resolution: {integrity: sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==} dev: true + + /zod@3.23.0-beta.2: + resolution: {integrity: sha512-u5wv4OKNPwybJMpVsBZc2LZq5DEk+64xloBnD/QwvfdLimLE0+3cqxMbOZzHwe44vseinF3DoHz7KCWV1MLfAg==} + dev: true diff --git a/src/create/schema/parsers/string.test.ts b/src/create/schema/parsers/string.test.ts index 7017050..3ae88d3 100644 --- a/src/create/schema/parsers/string.test.ts +++ b/src/create/schema/parsers/string.test.ts @@ -205,6 +205,9 @@ describe('createStringSchema', () => { ${z.string().email()} | ${'email'} ${z.string().url()} | ${'uri'} ${z.string().datetime()} | ${'date-time'} + ${z.string().date()} | ${'date'} + ${z.string().time()} | ${'time'} + ${z.string().duration()} | ${'duration'} `( 'creates a string schema with $format', ({ zodString, format }: { zodString: ZodString; format: string }) => { diff --git a/src/create/schema/parsers/string.ts b/src/create/schema/parsers/string.ts index ee58da6..f346a5d 100644 --- a/src/create/schema/parsers/string.ts +++ b/src/create/schema/parsers/string.ts @@ -132,6 +132,18 @@ const mapStringFormat = ( return 'date-time'; } + if (zodStringChecks.date) { + return 'date'; + } + + if (zodStringChecks.time) { + return 'time'; + } + + if (zodStringChecks.duration) { + return 'duration'; + } + if (zodStringChecks.email) { return 'email'; }