Skip to content

docs: fix Zod checkbox guidance for remote form fields - #17111

Merged
Rich-Harris merged 1 commit into
sveltejs:version-3from
filiabel:docs/zod-checkbox-default-false
Sep 15, 2026
Merged

Rich-Harris merged 1 commit into
sveltejs:version-3from
filiabel:docs/zod-checkbox-default-false

Conversation

@filiabel

Copy link
Copy Markdown
Contributor

See this comment.

The documented z.coerce.boolean<boolean>() is broken for unchecked checkboxes, in two independent ways:

z.boolean().default(false) fixes both, mirrors the v.optional(v.boolean(), false) already documented for Valibot, and works on zod 4.3 and earlier, so no version caveat is needed. z.coerce was never needed here: checkbox fields are prefixed with b: and coerced by SvelteKit before validation, as this same page notes. A default rather than .optional() because the server omits the key entirely while the client preflight sends a literal false — .default(false) covers both.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

The documented `z.coerce.boolean<boolean>()` no longer works for an
unchecked checkbox, for two independent reasons:

- Runtime: zod 4.4.0 (colinhacks/zod#5661) tightened object-key
  optionality, so a `z.coerce.*` key is no longer optional. An absent
  key now fails with "expected nonoptional, received undefined", which
  is exactly the unchecked-checkbox case.
- Types: the `form()` guard added in sveltejs#15804 rejects it. The `<boolean>`
  generic declares the schema's *input* type as `boolean`, which is
  precisely what `HasNonOptionalBoolean` flags.

`z.boolean().default(false)` fixes both. It is the direct analogue of
the `v.optional(v.boolean(), false)` already documented for Valibot,
and it works on zod 4.3 and earlier too, so no version caveat is needed.

`z.coerce` was never required here: checkbox fields are prefixed with
`b:` and coerced by SvelteKit before validation, as this same page
already notes.

A default is used rather than `.optional()` because the two submit paths
disagree: the server omits the key entirely (`convert_formdata` iterates
`data.keys()`), while the client preflight sends a literal `false`.
`.default(false)` covers both; `.optional()` yields `undefined` on the
server and `false` on the client.
@pkg-svelte-dev

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from a8f00b4:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/a8f00b49d5d29c94500a403477d87cb6e9c8f994

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/kit/pr/17111

Note

This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed.

@changeset-bot

changeset-bot Bot commented Sep 15, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: a8f00b4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@Rich-Harris Rich-Harris left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thank you!

@Rich-Harris
Rich-Harris merged commit e8c6dd0 into sveltejs:version-3 Sep 15, 2026
4 of 5 checks passed
Nic-Polumeyv added a commit that referenced this pull request Nov 22, 2026
Fixes #17834

Remote form schemas currently reject required literal-true fields,
including Zod’s `z.literal(true)`. For a consent checkbox, rejecting an
unchecked submission is intentional, so requiring an optional field
defeats the schema’s purpose.

The guard introduced in
[#15804](#15804) remains useful for
ordinary booleans: unchecked checkboxes omit their value, and schemas
need to account for that. The Zod guidance corrected in
[#17111](#17111) still applies to
those fields.

Exempt a required literal `true` property, rather than removing the
safeguard or changing submission coercion. This covers nested objects
and Standard Schema validators generally, while retaining rejection of
required general booleans, literal `false` and `true[]`.
Optional/default fields and the `any` recursion guard remain unchanged.
Existing shared field handling
([#16940](#16940)) already supplies
checked values correctly; no runtime validation or serialization changes
are needed.

The diagnostic and documentation now distinguish required consent from
ordinary boolean fields. Regression coverage exercises schema inference
and checkbox field typing as well as recursive cases.

- [Ported
reproduction](https://www.sveltelab.dev/#code=N4IgdA5glgLlEDsD2AnApiAXKAZlANhtiAMZIIxoUDOWIyAJmgPoC2SDArodQDoL8AxAAIA8pxgAHCfzBIJ0mLIBuaFCTT5ZCNDHxQcAT1kB3FAEMEEQin4B6MNVX5KAWgDWsewCNOBBvxCYgDKsgAiwczBMKho-AAqABacrN7UYAzegQgiAKIIyrJUhQhgxWAAVPwAhGUFZQAe5qyShDV1ymCU1EoCOcIAarBxBcNgZAh4EGAAVulwrGg9zZKuVaOU4%2BRTXfNQi8sta-wgAL6nADQg1OpYoAxQ6CQxKIZ3IOaSkmCJMKz47zwhHeE0oNDoAB5qgwkM9DJI0MJfv8AHz8CHI-DCfCWCAAXl4ICohLRAhgGLQ5gYpN4SnJixg5mEJES5hQ1F0BJAEhwrgAHIThHYaXSIQymQhmmgucooGgTJJUEoQMzyGCYFyTFAGDBEnimLKNK4tTrEhdhFAELAoOZ8K5qCRbdKAIyC4WBUXi4SSxZcygNGD2x1tFWgqgawkOp1ukW0gCkTk0lE8MB%2BlIYcY9ELsiXTIoh3g4hmEDHMjPtzmTsFcknQ%2BCQVNcpcZXMSSFUthAsfJD2Uwh6hkIXIe1Fa5kMmFVFHDfC7CrcraBTYELDEMcezvfzdhXhlJ2cxpLOlxAKHk3XeDyeLzexAA1JJzBA0I4F4CCERQGHwZgQAAlTgIMIAAGCCSKwwgoABzK5iQ7hAdkwRoIiQHUPACCcN86DsJsMDUPBCBHlcqGIBhYBYUgOG0MQQIfqQaoznQ%2ByKigMDCMAwg4Kg4GnBxp7gQA5AAJJ8kh2ByKAdvxADc-BMUqbHCAAXsIPE4Hxwj8YpHDSdkaANMxrETD0-ZoRhwh4hxXEABT8MISlyN4MxoM8VnALZdnCI%2B6AsDE1CTopYD6JQFj4FZMCQWgACUFzuac0XueY1CGAgJDCFZkXmSibGnPwkUyQR5yFceD6wY%2Bz5zOQb7AsQX64XQbmAcIhI%2BnEWBNSAKauM6ADsfIAMwACyEjFjWEh2qHkISk6EgADGAc2uiAI12YStZQMoZatZO4WcGgy3tTA8Jbe17BcCG%2B2RiQKBQJIuFTWx7krSABr3WNwwlmghRLY97Uss57ivdcC4eLA-bJalABkEP9sDf2wcIriuLhExTMIYB2Mj2zwLM1CTSAsUXc9n1hGgCIIEwKVyrOk4NR57UAAKJi4aBzHYVKfMFrjmBISCA3y83zcNP2EozC6symgN9WAA2C99jVPaLSYs9Qdiym4rScNACAVkrgPdWAUuLftT1M5QgMAKxgObACcYBG8LQNK64cMA21hIAHoy-rApy3ThKHQiDrXbdgPuwAbALfVC-L7Vq8dhL81LPvG%2B1WkBG7IAyxH3WErF-A5QVx6Y5M2MVQR1HviC9HfiADW0oSelggw1PtcJXwYw6WMQNH9d0S074oKIt1QOQLd13SkbhVAzz3TtIy0pcHqEpaJD4JwTD3QA2pG6jDbHYwo9jd0gAAuvnJznFccdbCX0x1RX1WftX98gHJLEKabi6g6p6n8YrzPi1gKrYYOkrQtHkuxJgeAdAAGEu4qV4kgAScdQH8D0gZD6OBuYuEwZaNAcDb6uWEBrLWflhCb0-imdKJ5QvucIAA)

<!--
svelte-triage-bot:feedback-baseline:81dc08a12b2bf1f429d63ffe858fb9e05256d947
-->

---------

Co-authored-by: svelte-triage-bot <[email protected]>
Co-authored-by: Nic Polumeyv <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants