Skip to content

Commit 4e73c03

Browse files
authoredMay 17, 2023
feat(zod): add nullable & nullish to output (#856)
1 parent d415077 commit 4e73c03

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed
 

‎packages/zod/src/index.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@ const generateZodValidationSchemaDefinition = (
5858
const consts = [];
5959
const functions: [string, any][] = [];
6060
const type = resolveZodType(schema.type);
61-
const required =
62-
schema.default !== undefined
63-
? false
64-
: _required ?? !schema.nullable ?? false;
61+
const required = schema.default !== undefined ? false : _required ?? false;
62+
const nullable = schema.nullable ?? false;
6563
const min =
6664
schema.minimum ?? schema.exclusiveMinimum ?? schema.minLength ?? undefined;
6765
const max =
@@ -200,7 +198,11 @@ const generateZodValidationSchemaDefinition = (
200198
]);
201199
}
202200

203-
if (!required) {
201+
if (!required && nullable) {
202+
functions.push(['nullish', undefined]);
203+
} else if (nullable) {
204+
functions.push(['nullable', undefined]);
205+
} else if (!required) {
204206
functions.push(['optional', undefined]);
205207
}
206208

1 commit comments

Comments
 (1)

vercel[bot] commented on May 17, 2023

@vercel[bot]
Please sign in to comment.