Skip to content

Commit

Permalink
Zod fixes for 770, 768 and 767 (#771)
Browse files Browse the repository at this point in the history
* fix: zod with min/max

* fix: uri fix Zod

* fix: zod correct use of required

* chore: add test cases for recursive
  • Loading branch information
stijnvanhulle authored Jan 14, 2024
1 parent d308d1c commit 6165dda
Show file tree
Hide file tree
Showing 25 changed files with 431 additions and 322 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-teachers-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/swagger-zod": patch
---

Optional properties with type: array allow it's values to be undefined in zod shape
5 changes: 5 additions & 0 deletions .changeset/soft-feet-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/swagger-zod": patch
---

uri should not generate an URL for Zod
5 changes: 5 additions & 0 deletions .changeset/weak-geckos-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/swagger-zod": patch
---

min/max with schema.type == string
4 changes: 2 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
"@vercel/analytics": "^1.1.1",
"sitemap": "^7.1.1",
"vitepress": "^1.0.0-rc.36",
"vue": "^3.4.8"
"vue": "^3.4.13"
},
"devDependencies": {
"@types/node": "^20.10.8"
"@types/node": "^20.11.0"
},
"packageManager": "[email protected]",
"engines": {
Expand Down
12 changes: 6 additions & 6 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@
"@kubb/swagger-ts": "workspace:*",
"@kubb/swagger-zod": "workspace:*",
"@kubb/swagger-zodios": "workspace:*",
"@tanstack/react-query": "^5.17.9",
"@tanstack/solid-query": "^5.17.9",
"@tanstack/svelte-query": "^5.17.9",
"@tanstack/vue-query": "^5.17.9",
"@tanstack/react-query": "^5.17.10",
"@tanstack/solid-query": "^5.17.10",
"@tanstack/svelte-query": "^5.17.10",
"@tanstack/vue-query": "^5.17.10",
"@zodios/core": "^10.9.6",
"axios": "^1.6.5",
"msw": "^1.3.2",
"react": "^18.2.0",
"solid-js": "^1.8.10",
"solid-js": "^1.8.11",
"svelte": "^3.59.2",
"swr": "^2.2.4",
"tsup": "^8.0.1",
"vue": "^3.4.8",
"vue": "^3.4.13",
"zod": "^3.22.4"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions examples/advanced/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
"axios": "^1.6.5",
"msw": "^1.3.2",
"react": "^18.2.0",
"solid-js": "^1.8.10",
"solid-js": "^1.8.11",
"svelte": "^3.59.2",
"swr": "^2.2.4",
"vue": "^3.4.8",
"vue": "^3.4.13",
"zod": "^3.22.4"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/msw-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@kubb/swagger-msw": "workspace:*",
"@kubb/swagger-ts": "workspace:*",
"@mswjs/http-middleware": "^0.9.2",
"msw": "^2.0.13",
"msw": "^2.0.14",
"react": "^18.2.0",
"tsup": "^8.0.1"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/react-query-v5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@kubb/swagger-client": "workspace:*",
"@kubb/swagger-tanstack-query": "workspace:*",
"@kubb/swagger-ts": "workspace:*",
"@tanstack/react-query": "^5.17.9",
"@tanstack/react-query": "^5.17.10",
"@tanstack/react-query-devtools": "5.0.0",
"axios": "^1.6.5",
"react": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/solid-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@kubb/swagger-zod": "workspace:*",
"@tanstack/solid-query": "^4.36.1",
"axios": "^1.6.5",
"solid-js": "^1.8.10",
"solid-js": "^1.8.11",
"tsup": "^8.0.1"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions examples/vue-query-v5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
"@kubb/swagger-tanstack-query": "workspace:*",
"@kubb/swagger-ts": "workspace:*",
"@kubb/swagger-zod": "workspace:*",
"@tanstack/vue-query": "^5.17.9",
"@tanstack/vue-query": "^5.17.10",
"axios": "^1.6.5",
"vue": "^3.4.8"
"vue": "^3.4.13"
},
"devDependencies": {
"@vitejs/plugin-vue": "^4.6.2",
Expand Down
2 changes: 1 addition & 1 deletion examples/vue-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@kubb/swagger-zod": "workspace:*",
"@tanstack/vue-query": "^4.37.1",
"axios": "^1.6.5",
"vue": "^3.4.8"
"vue": "^3.4.13"
},
"devDependencies": {
"@vitejs/plugin-vue": "^4.6.2",
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@
"@changesets/cli": "^2.27.1",
"@kubb/eslint-config": "workspace:*",
"@kubb/ts-config": "workspace:*",
"@types/node": "^20.10.8",
"@vitest/coverage-v8": "^1.1.3",
"@vitest/ui": "^1.1.3",
"@types/node": "^20.11.0",
"@vitest/coverage-v8": "^1.2.0",
"@vitest/ui": "^1.2.0",
"bun-types": "^1.0.22",
"dprint": "^0.45.0",
"prettier": "^3.1.1",
"prettier": "^3.2.2",
"prettier-eslint": "^16.2.0",
"rimraf": "^5.0.5",
"ts-node": "^10.9.2",
"turbo": "^1.11.3",
"typescript": "^5.3.3",
"vite-tsconfig-paths": "^4.2.3",
"vitest": "^1.1.3"
"vitest": "^1.2.0"
},
"namespace": "@kubb",
"packageManager": "[email protected]",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@kubb/swagger": "workspace:*",
"@kubb/ts-config": "workspace:*",
"@kubb/tsup-config": "workspace:*",
"@types/node": "^20.10.8",
"@types/node": "^20.11.0",
"source-map-support": "^0.5.21",
"tsup": "^8.0.1",
"typescript": "^5.3.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/config/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
},
"dependencies": {
"@eslint/js": "^8.56.0",
"@types/eslint": "~8.56.1",
"@types/eslint": "~8.56.2",
"@typescript-eslint/eslint-plugin": "^6.18.1",
"@typescript-eslint/parser": "~6.15.0",
"eslint-config-prettier": "~9.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/config/tsup-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"devDependencies": {
"@kubb/ts-config": "workspace:*",
"@types/node": "^20.10.8",
"@types/node": "^20.11.0",
"tsup": "^8.0.1"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"dependencies": {
"@kubb/parser": "workspace:*",
"@kubb/types": "workspace:*",
"change-case": "^5.4.0",
"change-case": "^5.4.1",
"directory-tree": "^3.5.1",
"find-up": "^7.0.0",
"fs-extra": "^11.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/kubb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"devDependencies": {
"@kubb/ts-config": "workspace:*",
"@kubb/tsup-config": "workspace:*",
"@types/node": "^20.10.8",
"@types/node": "^20.11.0",
"tsup": "^8.0.1",
"typescript": "^5.3.3"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/swagger-zod/mocks/petStore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ components:
type: string
tag:
type: string
maxLength: 100
minLength: 5

OptionalPet:
type: object
Expand Down
28 changes: 28 additions & 0 deletions packages/swagger-zod/mocks/recursive.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
openapi: 3.0.1
info:
title: Example Schema
servers:
- url: http://localhost/
paths:
"/example":
get:
responses:
'200':
description: Ok
content:
application/json:
schema:
"$ref": "#/components/schemas/Example"
components:
schemas:
Example:
type: object
required: ['name', 'children']
properties:
name:
type: string
children:
type: array
items:
$ref: "#/components/schemas/Example"
28 changes: 25 additions & 3 deletions packages/swagger-zod/src/ZodGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ZodGenerator } from './ZodGenerator.ts'

import type { OasTypes } from '@kubb/swagger/oas'

describe('ZodGenerator simple', async () => {
describe('ZodGenerator PetStore', async () => {
const petStorePath = path.resolve(__dirname, '../mocks/petStore.yaml')
const oas = await new OasManager().parse(petStorePath)

Expand Down Expand Up @@ -63,7 +63,7 @@ describe('ZodGenerator simple', async () => {
})
})

describe('ZodGenerator with const', async () => {
describe('ZodGenerator constCases', async () => {
const discriminatorPath = path.resolve(__dirname, '../mocks/constCases.yaml')
const oas = await new OasManager().parse(discriminatorPath)
const generator = new ZodGenerator({
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('ZodGenerator with const', async () => {
})
})

describe('ZodGenerator lazy import', async () => {
describe('ZodGenerator lazy', async () => {
const petStorePath = path.resolve(__dirname, '../mocks/lazy.yaml')
const oas = await new OasManager().parse(petStorePath)

Expand Down Expand Up @@ -184,3 +184,25 @@ describe('ZodGenerator enums', async () => {
expect(node).toMatchSnapshot()
})
})

describe('ZodGenerator recursive', async () => {
const petStorePath = path.resolve(__dirname, '../mocks/recursive.yaml')
const oas = await new OasManager().parse(petStorePath)

test('generate schema for Example', async () => {
const generator = new ZodGenerator({
exclude: undefined,
include: undefined,
override: undefined,
transformers: {},
}, {
oas,
pluginManager: mockedPluginManager,
})

const schemas = oas.getDefinition().components?.schemas
const node = generator.build({ schema: schemas?.Example as OasTypes.SchemaObject, baseName: 'Example' })

expect(node).toMatchSnapshot()
})
})
8 changes: 4 additions & 4 deletions packages/swagger-zod/src/ZodGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export class ZodGenerator extends Generator<PluginOptions['resolvedOptions'], Co
}

if ('items' in schema) {
if (!schema.required && !baseName) {
if (!schema.required?.length && !baseName) {
baseItems.push({ keyword: zodKeywords.optional })
}

Expand All @@ -321,7 +321,7 @@ export class ZodGenerator extends Generator<PluginOptions['resolvedOptions'], Co

if (schema.properties || schema.additionalProperties) {
// properties -> literal type
if (!schema.required) {
if (!schema.required?.length) {
baseItems.push({ keyword: zodKeywords.optional })
}

Expand Down Expand Up @@ -363,7 +363,7 @@ export class ZodGenerator extends Generator<PluginOptions['resolvedOptions'], Co
baseItems.unshift({ keyword: zodKeywords.readOnly })
}

if (schema.type === zodKeywords.number || schema.type === zodKeywords.integer) {
if (schema.type === zodKeywords.number || schema.type === zodKeywords.integer || schema.type === zodKeywords.string) {
const min = schema.minimum ?? schema.minLength ?? schema.minItems ?? undefined
const max = schema.maximum ?? schema.maxLength ?? schema.maxItems ?? undefined

Expand Down Expand Up @@ -392,7 +392,7 @@ export class ZodGenerator extends Generator<PluginOptions['resolvedOptions'], Co
baseItems.unshift({ keyword: zodKeywords.email })
}

if (schema.format === 'uri' || schema.format === 'hostname') {
if (schema.format === 'hostname') {
baseItems.unshift({ keyword: zodKeywords.url })
}
if (schema.format === 'uuid') {
Expand Down
Loading

1 comment on commit 6165dda

@vercel
Copy link

@vercel vercel bot commented on 6165dda Jan 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kubb – ./

kubb-kubb.vercel.app
www.kubb.dev
kubb.dev
kubb-git-main-kubb.vercel.app

Please sign in to comment.