Skip to content

Commit

Permalink
Add ip format support (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
samchungy authored Dec 16, 2024
1 parent e953eb6 commit 34e37a6
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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+
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions src/create/schema/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ describe('enhanceWithMetadata', () => {
"spa": [Function],
"superRefine": [Function],
"transform": [Function],
"~standard": {
"validate": [Function],
"vendor": "zod",
"version": 1,
},
},
"typeName": "ZodCatch",
},
Expand Down Expand Up @@ -310,6 +315,11 @@ describe('enhanceWithMetadata', () => {
"spa": [Function],
"superRefine": [Function],
"transform": [Function],
"~standard": {
"validate": [Function],
"vendor": "zod",
"version": 1,
},
},
},
{
Expand Down Expand Up @@ -359,6 +369,11 @@ describe('enhanceWithMetadata', () => {
"spa": [Function],
"superRefine": [Function],
"transform": [Function],
"~standard": {
"validate": [Function],
"vendor": "zod",
"version": 1,
},
},
"typeName": "ZodDefault",
},
Expand Down Expand Up @@ -386,6 +401,11 @@ describe('enhanceWithMetadata', () => {
"spa": [Function],
"superRefine": [Function],
"transform": [Function],
"~standard": {
"validate": [Function],
"vendor": "zod",
"version": 1,
},
},
},
],
Expand Down Expand Up @@ -614,6 +634,11 @@ describe('enhanceWithMetadata', () => {
"spa": [Function],
"superRefine": [Function],
"transform": [Function],
"~standard": {
"validate": [Function],
"vendor": "zod",
"version": 1,
},
},
"typeName": "ZodCatch",
},
Expand Down Expand Up @@ -641,6 +666,11 @@ describe('enhanceWithMetadata', () => {
"spa": [Function],
"superRefine": [Function],
"transform": [Function],
"~standard": {
"validate": [Function],
"vendor": "zod",
"version": 1,
},
},
},
{
Expand Down Expand Up @@ -690,6 +720,11 @@ describe('enhanceWithMetadata', () => {
"spa": [Function],
"superRefine": [Function],
"transform": [Function],
"~standard": {
"validate": [Function],
"vendor": "zod",
"version": 1,
},
},
"typeName": "ZodDefault",
},
Expand Down Expand Up @@ -717,6 +752,11 @@ describe('enhanceWithMetadata', () => {
"spa": [Function],
"superRefine": [Function],
"transform": [Function],
"~standard": {
"validate": [Function],
"vendor": "zod",
"version": 1,
},
},
},
],
Expand Down
20 changes: 12 additions & 8 deletions src/create/schema/parsers/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down
16 changes: 16 additions & 0 deletions src/create/schema/parsers/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down

0 comments on commit 34e37a6

Please sign in to comment.