Skip to content

Commit

Permalink
fix(zod): array min max issues (#1052)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware authored Nov 15, 2023
1 parent 67856c2 commit 5b29452
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/zod/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,21 @@ 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,
name: string,
): { 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;
Expand Down Expand Up @@ -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('/');
Expand Down
23 changes: 23 additions & 0 deletions tests/specifications/circular.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5b29452

Please sign in to comment.