Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MinItems and maxItems for arrays are no longer included in zod schema #807

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/old-brooms-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/swagger-zod": patch
---

array type with min/max
4 changes: 2 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
"dependencies": {
"@vercel/analytics": "^1.1.2",
"sitemap": "^7.1.1",
"vitepress": "^1.0.0-rc.39",
"vitepress": "^1.0.0-rc.40",
"vue": "^3.4.15"
},
"devDependencies": {
"@types/node": "^20.11.5"
"@types/node": "^20.11.7"
},
"packageManager": "[email protected]",
"engines": {
Expand Down
6 changes: 3 additions & 3 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false"
},
"dependencies": {
"@faker-js/faker": "^8.3.1",
"@faker-js/faker": "^8.4.0",
"@kubb/cli": "workspace:*",
"@kubb/core": "workspace:*",
"@kubb/swagger": "workspace:*",
Expand All @@ -37,10 +37,10 @@
"@tanstack/svelte-query": "^5.17.19",
"@tanstack/vue-query": "^5.17.19",
"@zodios/core": "^10.9.6",
"axios": "^1.6.5",
"axios": "^1.6.7",
"msw": "^1.3.2",
"react": "^18.2.0",
"solid-js": "^1.8.11",
"solid-js": "^1.8.12",
"svelte": "^3.59.2",
"swr": "^2.2.4",
"tsup": "^8.0.1",
Expand Down
6 changes: 3 additions & 3 deletions examples/advanced/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false"
},
"dependencies": {
"@faker-js/faker": "^8.3.1",
"@faker-js/faker": "^8.4.0",
"@kubb/cli": "workspace:*",
"@kubb/core": "workspace:*",
"@kubb/swagger": "workspace:*",
Expand All @@ -41,10 +41,10 @@
"@tanstack/svelte-query": "^4.36.1",
"@tanstack/vue-query": "^4.37.1",
"@zodios/core": "^10.9.6",
"axios": "^1.6.5",
"axios": "^1.6.7",
"msw": "^1.3.2",
"react": "^18.2.0",
"solid-js": "^1.8.11",
"solid-js": "^1.8.12",
"svelte": "^3.59.2",
"swr": "^2.2.4",
"vue": "^3.4.15",
Expand Down
2 changes: 2 additions & 0 deletions examples/advanced/petStore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ paths:
application/json:
schema:
type: array
minItems: 1
maxItems: 3
items:
$ref: "#/components/schemas/Pet"
application/xml:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ export const findPetsByStatusQueryParamsSchema = z.object({
/**
* @description successful operation
*/
export const findPetsByStatusQueryResponseSchema = z.array(z.lazy(() => petSchema))
export const findPetsByStatusQueryResponseSchema = z.array(z.lazy(() => petSchema)).min(1).max(3)
2 changes: 1 addition & 1 deletion examples/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@kubb/swagger": "workspace:*",
"@kubb/swagger-client": "workspace:*",
"@kubb/swagger-ts": "workspace:*",
"axios": "^1.6.5"
"axios": "^1.6.7"
},
"devDependencies": {
"@kubb/ts-config": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion examples/faker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false"
},
"dependencies": {
"@faker-js/faker": "^8.3.1",
"@faker-js/faker": "^8.4.0",
"@kubb/cli": "workspace:*",
"@kubb/core": "workspace:*",
"@kubb/swagger": "workspace:*",
Expand Down
4 changes: 2 additions & 2 deletions examples/msw-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false"
},
"dependencies": {
"@faker-js/faker": "^8.3.1",
"@faker-js/faker": "^8.4.0",
"@kubb/cli": "workspace:*",
"@kubb/core": "workspace:*",
"@kubb/swagger": "workspace:*",
Expand All @@ -33,7 +33,7 @@
"@kubb/swagger-msw": "workspace:*",
"@kubb/swagger-ts": "workspace:*",
"@mswjs/http-middleware": "^0.9.2",
"msw": "^2.1.2",
"msw": "^2.1.5",
"react": "^18.2.0",
"tsup": "^8.0.1"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/msw/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false"
},
"dependencies": {
"@faker-js/faker": "^8.3.1",
"@faker-js/faker": "^8.4.0",
"@kubb/cli": "workspace:*",
"@kubb/core": "workspace:*",
"@kubb/swagger": "workspace:*",
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 @@ -31,7 +31,7 @@
"@kubb/swagger-ts": "workspace:*",
"@tanstack/react-query": "^5.17.19",
"@tanstack/react-query-devtools": "5.0.0",
"axios": "^1.6.5",
"axios": "^1.6.7",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"tsup": "^8.0.1"
Expand Down
2 changes: 1 addition & 1 deletion examples/react-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@kubb/swagger-ts": "workspace:*",
"@tanstack/react-query": "^4.36.1",
"@tanstack/react-query-devtools": "^4.36.1",
"axios": "^1.6.5",
"axios": "^1.6.7",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"tsup": "^8.0.1"
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-single/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@kubb/swagger-tanstack-query": "workspace:*",
"@kubb/swagger-ts": "workspace:*",
"@tanstack/react-query": "^4.36.1",
"axios": "^1.6.5",
"axios": "^1.6.7",
"react": "^18.2.0",
"tsup": "^8.0.1"
}
Expand Down
4 changes: 2 additions & 2 deletions examples/solid-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"@kubb/swagger-ts": "workspace:*",
"@kubb/swagger-zod": "workspace:*",
"@tanstack/solid-query": "^4.36.1",
"axios": "^1.6.5",
"solid-js": "^1.8.11",
"axios": "^1.6.7",
"solid-js": "^1.8.12",
"tsup": "^8.0.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/svelte-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@kubb/swagger-ts": "workspace:*",
"@kubb/swagger-zod": "workspace:*",
"@tanstack/svelte-query": "^4.36.1",
"axios": "^1.6.5",
"axios": "^1.6.7",
"svelte": "^3.59.2",
"tsup": "^8.0.1"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/swr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@kubb/swagger-swr": "workspace:*",
"@kubb/swagger-ts": "workspace:*",
"@kubb/swagger-zod": "workspace:*",
"axios": "^1.6.5",
"axios": "^1.6.7",
"react": "^18.2.0",
"swr": "^2.2.4",
"tsup": "^8.0.1"
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@kubb/core": "workspace:*",
"@kubb/swagger": "workspace:*",
"@kubb/swagger-ts": "workspace:*",
"axios": "^1.6.5",
"axios": "^1.6.7",
"tsup": "^8.0.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/vue-query-v5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@kubb/swagger-ts": "workspace:*",
"@kubb/swagger-zod": "workspace:*",
"@tanstack/vue-query": "^5.17.19",
"axios": "^1.6.5",
"axios": "^1.6.7",
"vue": "^3.4.15"
},
"devDependencies": {
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 @@ -30,7 +30,7 @@
"@kubb/swagger-ts": "workspace:*",
"@kubb/swagger-zod": "workspace:*",
"@tanstack/vue-query": "^4.37.1",
"axios": "^1.6.5",
"axios": "^1.6.7",
"vue": "^3.4.15"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/zodios/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@kubb/swagger-zodios": "workspace:*",
"@zodios/core": "^10.9.6",
"@zodios/express": "^10.6.1",
"axios": "^1.6.5",
"axios": "^1.6.7",
"cors": "^2.8.5",
"express": "^4.18.2",
"react": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"@changesets/cli": "^2.27.1",
"@kubb/eslint-config": "workspace:*",
"@kubb/ts-config": "workspace:*",
"@types/node": "^20.11.5",
"@types/node": "^20.11.7",
"@vitest/coverage-v8": "^1.2.1",
"@vitest/ui": "^1.2.1",
"bun-types": "^1.0.25",
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"cac": "^6.7.14",
"chokidar": "^3.5.3",
"cosmiconfig": "^9.0.0",
"esbuild": "^0.19.11",
"esbuild": "^0.19.12",
"execa": "^8.0.1",
"ora": "^8.0.1",
"pretty-error": "^4.0.0",
Expand All @@ -66,7 +66,7 @@
"@kubb/swagger": "workspace:*",
"@kubb/ts-config": "workspace:*",
"@kubb/tsup-config": "workspace:*",
"@types/node": "^20.11.5",
"@types/node": "^20.11.7",
"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 @@ -45,7 +45,7 @@
"dependencies": {
"@eslint/js": "^8.56.0",
"@types/eslint": "~8.56.2",
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "~6.15.0",
"eslint-config-prettier": "~9.1.0",
"eslint-config-turbo": "^1.11.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/config/tsup-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false"
},
"dependencies": {
"@microsoft/api-extractor": "^7.39.1",
"@microsoft/api-extractor": "^7.39.4",
"esbuild-plugin-file-path-extensions": "^2.0.0",
"fast-glob": "^3.3.2",
"tinyrainbow": "^1.1.1"
},
"devDependencies": {
"@kubb/ts-config": "workspace:*",
"@types/node": "^20.11.5",
"@types/node": "^20.11.7",
"tsup": "^8.0.1"
},
"peerDependencies": {
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.11.5",
"@types/node": "^20.11.7",
"tsup": "^8.0.1",
"typescript": "^5.3.3"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"@kubb/ts-config": "workspace:*",
"@kubb/tsup-config": "workspace:*",
"@types/react": "^18.2.48",
"axios": "^1.6.5",
"axios": "^1.6.7",
"eslint": "^8.56.0",
"react": "^18.2.0",
"tsup": "^8.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-faker/src/FakerGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ export class FakerGenerator extends Generator<PluginOptions['resolvedOptions'],
].filter(Boolean)
}

if (schema.type === fakerKeywords.number || schema.type === fakerKeywords.integer) {
if ([fakerKeywords.number as string, fakerKeywords.integer as string].includes(schema.type)) {
const min = schema.minimum ?? schema.minLength ?? schema.minItems ?? undefined
const max = schema.maximum ?? schema.maxLength ?? schema.maxItems ?? undefined

Expand Down
8 changes: 7 additions & 1 deletion packages/swagger-zod/mocks/petStore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ components:
type: string
maxLength: 100
minLength: 5

PetArray:
description: List of Pet object
type: array
minItems: 1
maxItems: 3
items:
$ref: '#/components/schemas/Pet'
OptionalPet:
type: object
properties:
Expand Down
20 changes: 20 additions & 0 deletions packages/swagger-zod/src/ZodGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ describe('ZodGenerator PetStore', async () => {

expect(node).toMatchSnapshot()
})

test('generate schema for PetArray', async () => {
const generator = new ZodGenerator({
exclude: undefined,
include: undefined,
override: undefined,
transformers: {},
typed: false,
dateType: 'string',
unknownType: 'any',
}, {
oas,
pluginManager: mockedPluginManager,
})

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

expect(node).toMatchSnapshot()
})
})

describe('ZodGenerator constCases', async () => {
Expand Down
13 changes: 12 additions & 1 deletion packages/swagger-zod/src/ZodGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,17 @@ export class ZodGenerator extends Generator<PluginOptions['resolvedOptions'], Co
}

if ('items' in schema) {
const min = schema.minimum ?? schema.minLength ?? schema.minItems ?? undefined
const max = schema.maximum ?? schema.maxLength ?? schema.maxItems ?? undefined

if (max !== undefined) {
baseItems.unshift({ keyword: zodKeywords.max, args: max })
}

if (min !== undefined) {
baseItems.unshift({ keyword: zodKeywords.min, args: min })
}

// items -> array
return [{ keyword: zodKeywords.array, args: this.getTypeFromSchema(schema.items as OasTypes.SchemaObject, baseName) }, ...baseItems]
}
Expand Down Expand Up @@ -403,7 +414,7 @@ export class ZodGenerator extends Generator<PluginOptions['resolvedOptions'], Co
baseItems.unshift({ keyword: zodKeywords.readOnly })
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ exports[`ZodGenerator PetStore > generate schema for Pet 1`] = `
]
`;

exports[`ZodGenerator PetStore > generate schema for PetArray 1`] = `
[
"export const PetArray = z.array(z.lazy(() => Pet)).min(1).max(3).describe(\`List of Pet object\`);",
]
`;

exports[`ZodGenerator PetStore > generate schema for Pets 1`] = `
[
"export const Pets = z.array(z.object({"id": z.number(),"name": z.string(),"tag": z.string().optional()}));",
Expand Down
4 changes: 2 additions & 2 deletions packages/unplugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@
"@kubb/tsup-config": "workspace:*",
"@nuxt/kit": "^3.9.3",
"@nuxt/schema": "^3.9.3",
"@types/node": "^20.11.5",
"@types/node": "^20.11.7",
"eslint": "^8.56.0",
"rimraf": "^5.0.5",
"rollup": "^4.9.6",
"tsup": "^8.0.1",
"typescript": "^5.3.3",
"vite": "^5.0.12",
"vitest": "^1.2.1",
"webpack": "^5.89.0"
"webpack": "^5.90.0"
},
"packageManager": "[email protected]",
"engines": {
Expand Down
Loading
Loading