From 34e37a60387498f220ac885e7e114b62ab06dd28 Mon Sep 17 00:00:00 2001 From: Sam Chung Date: Mon, 16 Dec 2024 13:33:14 +1100 Subject: [PATCH] Add ip format support (#387) --- README.md | 2 +- package.json | 2 +- pnpm-lock.yaml | 12 +++---- src/create/schema/metadata.test.ts | 40 ++++++++++++++++++++++++ src/create/schema/parsers/string.test.ts | 20 +++++++----- src/create/schema/parsers/string.ts | 16 ++++++++++ 6 files changed, 76 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index adaea1c..30e48f8 100644 --- a/README.md +++ b/README.md @@ -805,7 +805,7 @@ As an example `z.string().nullable()` will be rendered differently - ZodSet - Treated as an array with `uniqueItems` (you may need to add a pre-process to convert it to a set) - ZodString - - `format` mapping for `.url()`, `.uuid()`, `.email()`, `.datetime()`, `.date()`, `.time()`, `.duration()` + - `format` mapping for `.url()`, `.uuid()`, `.email()`, `.datetime()`, `.date()`, `.time()`, `.duration()`, `.ip({ version: 'v4' })`, `.ip({ version: 'v6' })`, `.cidr({ version: 'v4' })`, `.cidr({ version: 'v6' })` - `minLength`/`maxLength` mapping for `.length()`, `.min()`, `.max()` - `pattern` mapping for `.regex()`, `.startsWith()`, `.endsWith()`, `.includes()` - `contentEncoding` mapping for `.base64()` for OpenAPI 3.1.0+ diff --git a/package.json b/package.json index 0b2a846..2e92f40 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,7 @@ "openapi3-ts": "4.4.0", "skuba": "9.1.0", "yaml": "2.6.1", - "zod": "3.23.8" + "zod": "3.24.1" }, "peerDependencies": { "zod": "^3.21.4" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 51d0f5d..5ea1859 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -33,8 +33,8 @@ importers: specifier: 2.6.1 version: 2.6.1 zod: - specifier: 3.23.8 - version: 3.23.8 + specifier: 3.24.1 + version: 3.24.1 packages: @@ -5598,8 +5598,8 @@ packages: resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==} engines: {node: '>=10'} - zod@3.23.8: - resolution: {integrity: sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==} + zod@3.24.1: + resolution: {integrity: sha512-muH7gBL9sI1nciMZV67X5fTKKBLtwpZ5VBp1vsOQzj1MhrBZ4wlVCm3gedKZWLp0Oyel8sIGfeiz54Su+OVT+A==} snapshots: @@ -11198,7 +11198,7 @@ snapshots: tsx: 4.18.0 typescript: 5.6.2 validate-npm-package-name: 6.0.0 - zod: 3.23.8 + zod: 3.24.1 transitivePeerDependencies: - '@babel/core' - '@jest/transform' @@ -12020,4 +12020,4 @@ snapshots: yocto-queue@0.1.0: {} - zod@3.23.8: {} + zod@3.24.1: {} diff --git a/src/create/schema/metadata.test.ts b/src/create/schema/metadata.test.ts index faf74ad..c415adf 100644 --- a/src/create/schema/metadata.test.ts +++ b/src/create/schema/metadata.test.ts @@ -283,6 +283,11 @@ describe('enhanceWithMetadata', () => { "spa": [Function], "superRefine": [Function], "transform": [Function], + "~standard": { + "validate": [Function], + "vendor": "zod", + "version": 1, + }, }, "typeName": "ZodCatch", }, @@ -310,6 +315,11 @@ describe('enhanceWithMetadata', () => { "spa": [Function], "superRefine": [Function], "transform": [Function], + "~standard": { + "validate": [Function], + "vendor": "zod", + "version": 1, + }, }, }, { @@ -359,6 +369,11 @@ describe('enhanceWithMetadata', () => { "spa": [Function], "superRefine": [Function], "transform": [Function], + "~standard": { + "validate": [Function], + "vendor": "zod", + "version": 1, + }, }, "typeName": "ZodDefault", }, @@ -386,6 +401,11 @@ describe('enhanceWithMetadata', () => { "spa": [Function], "superRefine": [Function], "transform": [Function], + "~standard": { + "validate": [Function], + "vendor": "zod", + "version": 1, + }, }, }, ], @@ -614,6 +634,11 @@ describe('enhanceWithMetadata', () => { "spa": [Function], "superRefine": [Function], "transform": [Function], + "~standard": { + "validate": [Function], + "vendor": "zod", + "version": 1, + }, }, "typeName": "ZodCatch", }, @@ -641,6 +666,11 @@ describe('enhanceWithMetadata', () => { "spa": [Function], "superRefine": [Function], "transform": [Function], + "~standard": { + "validate": [Function], + "vendor": "zod", + "version": 1, + }, }, }, { @@ -690,6 +720,11 @@ describe('enhanceWithMetadata', () => { "spa": [Function], "superRefine": [Function], "transform": [Function], + "~standard": { + "validate": [Function], + "vendor": "zod", + "version": 1, + }, }, "typeName": "ZodDefault", }, @@ -717,6 +752,11 @@ describe('enhanceWithMetadata', () => { "spa": [Function], "superRefine": [Function], "transform": [Function], + "~standard": { + "validate": [Function], + "vendor": "zod", + "version": 1, + }, }, }, ], diff --git a/src/create/schema/parsers/string.test.ts b/src/create/schema/parsers/string.test.ts index d85df98..3f4c6e7 100644 --- a/src/create/schema/parsers/string.test.ts +++ b/src/create/schema/parsers/string.test.ts @@ -202,14 +202,18 @@ describe('createStringSchema', () => { }); it.each` - zodString | format - ${z.string().uuid()} | ${'uuid'} - ${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'} + zodString | format + ${z.string().uuid()} | ${'uuid'} + ${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'} + ${z.string().ip({ version: 'v4' })} | ${'ipv4'} + ${z.string().ip({ version: 'v6' })} | ${'ipv6'} + ${z.string().cidr({ version: 'v4' })} | ${'ipv4'} + ${z.string().cidr({ version: 'v6' })} | ${'ipv6'} `( '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 b95b193..2062a0f 100644 --- a/src/create/schema/parsers/string.ts +++ b/src/create/schema/parsers/string.ts @@ -161,6 +161,22 @@ const mapStringFormat = ( return 'uri'; } + if (zodStringChecks.ip?.every((ip) => ip.version === 'v4')) { + return 'ipv4'; + } + + if (zodStringChecks.ip?.every((ip) => ip.version === 'v6')) { + return 'ipv6'; + } + + if (zodStringChecks.cidr?.every((ip) => ip.version === 'v4')) { + return 'ipv4'; + } + + if (zodStringChecks.cidr?.every((ip) => ip.version === 'v6')) { + return 'ipv6'; + } + return undefined; };