Skip to content

fix(plugin-zod): keep an array default as an array literal - #804

Merged
stijnvanhulle merged 2 commits into
kubb-labs:mainfrom
xeoneux:main
Aug 28, 2026
Merged

fix(plugin-zod): keep an array default as an array literal#804
stijnvanhulle merged 2 commits into
kubb-labs:mainfrom
xeoneux:main

Conversation

@xeoneux

@xeoneux xeoneux commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Fixes the array case in formatDefault, so a default: [] reached through a $ref no longer
becomes .default({}).

formatDefault branched on typeof value === 'object', which is also true for an array, so []
took the object path and returned '{}'. defaultLiteral only guarded this for nodes that narrow
to array, and a property referencing an array schema is a ref node, so it fell through and
produced z.array(...).default({}) — a schema that does not typecheck (TS2769).

Arrays are now handled before the object case:

export function formatDefault(value: unknown): string {
  if (typeof value === 'string') return stringify(value)
  // An array is also `typeof 'object'`, so it has to be handled before the object case.
  if (Array.isArray(value)) return JSON.stringify(value)
  if (typeof value === 'object' && value !== null) return '{}'

  return String(value ?? '')
}

Before / after for a property referencing an array schema:

- resources: resourcesSchema.optional().default({})
+ resources: resourcesSchema.optional().default([])

The fix is in formatDefault rather than defaultLiteral so it also covers any other path that
reaches the value without a narrowed array node. Plain objects still collapse to {}, unchanged.

@codesandbox

codesandbox Bot commented Aug 27, 2026

Copy link
Copy Markdown

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

@changeset-bot

changeset-bot Bot commented Aug 27, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8bc5aa3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@kubb/plugin-zod Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Aug 27, 2026
@xeoneux

xeoneux commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

Fixes #803

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Aug 28, 2026
@stijnvanhulle

Copy link
Copy Markdown
Contributor

@all-contributors please add @xeoneux for code

@allcontributors

Copy link
Copy Markdown
Contributor

@stijnvanhulle

I've put up a pull request to add @xeoneux! 🎉

@stijnvanhulle
stijnvanhulle merged commit b1a175d into kubb-labs:main Aug 28, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants