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

fix(zod): array min max issues #1052

Merged
merged 2 commits into from
Nov 15, 2023
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
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