Skip to content

Commit

Permalink
feat(zod): add nullable & nullish to output (#856)
Browse files Browse the repository at this point in the history
  • Loading branch information
harumaxy authored May 17, 2023
1 parent d415077 commit 4e73c03
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/zod/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ const generateZodValidationSchemaDefinition = (
const consts = [];
const functions: [string, any][] = [];
const type = resolveZodType(schema.type);
const required =
schema.default !== undefined
? false
: _required ?? !schema.nullable ?? false;
const required = schema.default !== undefined ? false : _required ?? false;
const nullable = schema.nullable ?? false;
const min =
schema.minimum ?? schema.exclusiveMinimum ?? schema.minLength ?? undefined;
const max =
Expand Down Expand Up @@ -200,7 +198,11 @@ const generateZodValidationSchemaDefinition = (
]);
}

if (!required) {
if (!required && nullable) {
functions.push(['nullish', undefined]);
} else if (nullable) {
functions.push(['nullable', undefined]);
} else if (!required) {
functions.push(['optional', undefined]);
}

Expand Down

1 comment on commit 4e73c03

@vercel
Copy link

@vercel vercel bot commented on 4e73c03 May 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.