Skip to content

Commit

Permalink
fix: discard optional if default is present
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoffer N authored and Christoffer N committed Oct 7, 2024
1 parent 9707a57 commit 08c384e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-teachers-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/plugin-zod": patch
---

Discard `optional()` if there is a `default()` to ensure the output type is not `T | undefined`
4 changes: 4 additions & 0 deletions packages/plugin-zod/src/components/Zod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ 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)
.filter((item) => {
if (hasTuple && (isKeyword(item, schemaKeywords.min) || isKeyword(item, schemaKeywords.max))) {
return false
}
if (hasDefault && isKeyword(item, schemaKeywords.optional)) {
return false
}

return true
})
Expand Down

0 comments on commit 08c384e

Please sign in to comment.