Skip to content

Commit

Permalink
fix(zod): allow blank value for non-required enums (#1113)
Browse files Browse the repository at this point in the history
* fix(zod): allow blank value for non-required enums

* fix(format): zod client

---------

Co-authored-by: Eric Butler <[email protected]>
  • Loading branch information
anymaniax and Eric Butler authored Dec 19, 2023
1 parent 1423d08 commit 1edc2ab
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions packages/zod/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,13 @@ const generateZodValidationSchemaDefinition = (
}

if (schema.enum && type !== 'number') {
functions.push([
'enum',
[
`[${schema.enum
.map((value) => (isString(value) ? `'${escape(value)}'` : `${value}`))
.join(', ')}]`,
],
]);
const enumValues = schema.enum.map((value) =>
isString(value) ? `'${escape(value)}'` : `${value}`,
);
if (!schema.required) {
enumValues.push(`""`);
}
functions.push(['enum', [`[${enumValues.join(', ')}]`]]);
}

if (!required && nullable) {
Expand Down

0 comments on commit 1edc2ab

Please sign in to comment.