Skip to content

Commit 79f6847

Browse files
author
Eric Butler
authoredSep 28, 2023
fix(zod): better handling for arrays, dates, min (#902)
1 parent 0296a82 commit 79f6847

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed
 

‎packages/zod/src/index.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@ const generateZodValidationSchemaDefinition = (
9090

9191
functions.push([type as string, undefined]);
9292

93-
if (schema.format === 'date-time' || schema.format === 'date') {
93+
if (schema.format === 'date') {
94+
functions.push(['regex', 'new RegExp(/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/)']);
95+
break;
96+
}
97+
98+
if (schema.format === 'date-time') {
9499
functions.push(['datetime', undefined]);
95100
break;
96101
}
@@ -177,8 +182,12 @@ const generateZodValidationSchemaDefinition = (
177182
}
178183

179184
if (min !== undefined) {
180-
consts.push(`export const ${name}Min = ${min};`);
181-
functions.push(['min', `${name}Min`]);
185+
if (min === 1) {
186+
functions.push(['min', `${min}`]);
187+
} else {
188+
consts.push(`export const ${name}Min = ${min};`);
189+
functions.push(['min', `${name}Min`]);
190+
}
182191
}
183192
if (max !== undefined) {
184193
consts.push(`export const ${name}Max = ${max};`);
@@ -281,6 +290,7 @@ const parseZodValidationSchemaDefinition = (
281290
}
282291
if (fn === 'array') {
283292
const value = args.functions.map(parseProperty).join('');
293+
consts += args.consts;
284294
return `.array(${value.startsWith('.') ? 'zod' : ''}${value})`;
285295
}
286296
return `.${fn}(${args})`;

1 commit comments

Comments
 (1)

vercel[bot] commented on Sep 28, 2023

@vercel[bot]
Please sign in to comment.