From 08c384ec2ae795fe36726ae00eb3dc65e7481677 Mon Sep 17 00:00:00 2001 From: Christoffer N Date: Fri, 4 Oct 2024 19:47:58 +0200 Subject: [PATCH] fix: discard optional if default is present --- .changeset/selfish-teachers-rhyme.md | 5 +++++ packages/plugin-zod/src/components/Zod.tsx | 4 ++++ 2 files changed, 9 insertions(+) create mode 100644 .changeset/selfish-teachers-rhyme.md diff --git a/.changeset/selfish-teachers-rhyme.md b/.changeset/selfish-teachers-rhyme.md new file mode 100644 index 000000000..2875fe4de --- /dev/null +++ b/.changeset/selfish-teachers-rhyme.md @@ -0,0 +1,5 @@ +--- +"@kubb/plugin-zod": patch +--- + +Discard `optional()` if there is a `default()` to ensure the output type is not `T | undefined` diff --git a/packages/plugin-zod/src/components/Zod.tsx b/packages/plugin-zod/src/components/Zod.tsx index 5c8908b22..dbcc47f19 100644 --- a/packages/plugin-zod/src/components/Zod.tsx +++ b/packages/plugin-zod/src/components/Zod.tsx @@ -19,6 +19,7 @@ type Props = { export function Zod({ name, typeName, tree, inferTypeName, mapper, coercion, keysToOmit, description }: Props): KubbNode { const hasTuple = tree.some((item) => isKeyword(item, schemaKeywords.tuple)) + const hasDefault = tree.some((item) => isKeyword(item, schemaKeywords.default)) const output = parserZod .sort(tree) @@ -26,6 +27,9 @@ export function Zod({ name, typeName, tree, inferTypeName, mapper, coercion, key if (hasTuple && (isKeyword(item, schemaKeywords.min) || isKeyword(item, schemaKeywords.max))) { return false } + if (hasDefault && isKeyword(item, schemaKeywords.optional)) { + return false + } return true })