Skip to content

fix(useSearchParams): keep string fields verbatim and parse single-item arrays - #437

Open
bardrop wants to merge 1 commit into
svecosystem:mainfrom
bardrop-oss:fix/search-params-string-and-single-array
Open

bardrop wants to merge 1 commit into
svecosystem:mainfrom
bardrop-oss:fix/search-params-string-and-single-array

Conversation

@bardrop

@bardrop bardrop commented Nov 14, 2026

Copy link
Copy Markdown

Fixes #436

What was wrong

extractParamValues and extractSelectiveParamValues turned "true"/"false" into booleans and [...]/{...} into parsed JSON for every field, before the schema had a say. String fields are written to the URL as-is, so a string like true or [1] came back as the wrong type:

  • Zod z.string().default("") failed validation and reset to "" (typing true into a bound input cleared it).
  • createSearchParamsSchema string fields ran String() on the parsed value, so [1] became "1" and {"a":1} became "[object Object]".

Array fields only used the comma fallback when the value had a comma, so ?tags=a stayed "a" and fell back to [].

Fix

  • extractSchemaInfo now also collects stringFields (fields whose default is a string, minus codec fields). Values for these are read verbatim.
  • Array fields split any non-empty value, so ?tags=a gives ["a"]. ?tags= still stays "", like before.
  • The per-value conversion was duplicated in both extract functions; it now lives in one convertParamValue helper so the two can't drift again. Behavior for other fields is unchanged (including a bad JSON value staying a plain string).

Tests

  • extract-param-values.test.ts: string fields keep true/false/[1]/{"a":1}/[]/{}, other fields still convert, single-item and empty array values.
  • validate-search-params.test.ts: the cases from the issue with both createSearchParamsSchema and Zod.

7 new tests fail on main and pass here. vitest --run: 250 unit tests pass (the browser project needs a local Chromium, which I don't have). prettier --check, oxlint, eslint and svelte-check are clean.

I left the changeset out since CONTRIBUTING says maintainers add those; happy to add one if you'd like.

…em arrays

String fields (default is a string) were pre-parsed before validation, so
?q=true became a boolean and ?q=[1] became an array. Zod string schemas then
reset the field to its default, and createSearchParamsSchema turned [1] into
"1" and {"a":1} into "[object Object]". String fields are now read as
written.

Array fields only used the comma fallback when the value had a comma, so
?tags=a stayed a string and failed validation. Any non-empty value is now
split, so ?tags=a gives ["a"].

The per-value conversion shared by extractParamValues and
extractSelectiveParamValues now lives in one helper.

Fixes svecosystem#436
@changeset-bot

changeset-bot Bot commented Nov 14, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 71922be

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

@bardrop

bardrop commented Nov 24, 2026 •

Copy link
Copy Markdown
Author

Friendly ping: one workflow run still needs a maintainer's approval to start. No conflicts. I left the changeset to you, as CONTRIBUTING says. Anything I should change?

@bardrop

bardrop commented Dec 2, 2026

Copy link
Copy Markdown
Author

Gentle ping: still mergeable, and I'm happy to adjust anything if you'd like it done another way.

This branch has not been deployed

No deployments
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.

useSearchParams: string fields coerce boolean/JSON-like values before validation and single-item comma-separated arrays fail

1 participant