diff --git a/packages/zod/src/index.ts b/packages/zod/src/index.ts index 0a0aec17d..c1e90d02e 100644 --- a/packages/zod/src/index.ts +++ b/packages/zod/src/index.ts @@ -49,9 +49,13 @@ const resolveZodType = (schemaTypeValue: SchemaObject['type']) => { } }; +// counter for unique naming +let counter = 0; + // https://github.com/colinhacks/zod#coercion-for-primitives const COERCEABLE_TYPES = ['string', 'number', 'boolean', 'bigint', 'date']; + const generateZodValidationSchemaDefinition = ( schema: SchemaObject | undefined, _required: boolean | undefined, @@ -59,7 +63,7 @@ const generateZodValidationSchemaDefinition = ( ): { functions: [string, any][]; consts: string[] } => { if (!schema) return { functions: [], consts: [] }; - const consts = []; + const consts: string[] = []; const functions: [string, any][] = []; const type = resolveZodType(schema.type); const required = schema.default !== undefined ? false : _required ?? false; @@ -188,13 +192,15 @@ const generateZodValidationSchemaDefinition = ( if (min === 1) { functions.push(['min', `${min}`]); } else { - consts.push(`export const ${name}Min = ${min};`); - functions.push(['min', `${name}Min`]); + counter++; + consts.push(`export const ${name}Min${counter} = ${min};\n`); + functions.push(['min', `${name}Min${counter}`]); } } if (max !== undefined) { - consts.push(`export const ${name}Max = ${max};`); - functions.push(['max', `${name}Max`]); + counter++; + consts.push(`export const ${name}Max${counter} = ${max};\n`); + functions.push(['max', `${name}Max${counter}`]); } if (matches) { const isStartWithSlash = matches.startsWith('/'); diff --git a/tests/specifications/circular.yaml b/tests/specifications/circular.yaml index 64db04bdd..a91fe1eae 100644 --- a/tests/specifications/circular.yaml +++ b/tests/specifications/circular.yaml @@ -13,6 +13,29 @@ paths: application/json: schema: $ref: '#/components/schemas/Node' + /list: + post: + summary: Add list + operationId: add-list + responses: + '200': + description: OK + requestBody: + content: + application/json: + schema: + type: object + properties: + list: + type: array + minItems: 1 + maxItems: 10 + items: + type: number + minimum: 1 + maximum: 10 + required: + - list parameters: - name: limit in: query