Array-ify repeated plain keys instead of keeping the last value - #3
Merged
Conversation
A field name repeated without brackets (e.g. a checkbox group) silently kept only the last value, while both of Formspark's own backend parsers (qs for url-encoded, Hono's parseBody for multipart) already treat a repeated key as an array. formson was the odd one out, and it's the library the docs recommend for exactly the case most likely to hit this: converting a native form's FormData to JSON before posting it (the Webflow integration guide). Bumped to 0.3.0: this changes the output shape for existing inputs, so it's treated as a minor bump rather than a patch.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
tag=first&tag=secondproduced{ tag: "second" }.qs.parse("tags=a&tags=b")(used for url-encoded submissions) and Hono'sparseBody({ all: true })(used for multipart submissions, monorepo PR #100) both already turn a repeated key into an array. formson was the only one of the three that didn't.formspark/documentation's Webflow integration guide tells developers to wire up in front of every form submission, since Webflow needs client-side JSON conversion — and native checkbox groups (a completely standard Webflow pattern) are exactly the shape that silently loses data through this path today.toJSONnow promotes a key to an array on its second plain occurrence ([existing, value]) and pushes on subsequent occurrences, while a plain key still correctly overwrites a nested object built via dot/bracket notation (unchanged "nested then scalar overwrites the object" behavior) — the object-vs-leaf distinction isexisting.constructor === Object, so this doesn't touch the bracket-array code path at all.items[](empty brackets) shares the exact same code path as a plain repeated key, so it now also produces an array instead of "last value wins" — arguably the more correct reading of that syntax anyway.Test plan
Filevalues (mirroring the existing bracket-array File test). 32 tests total, all passing.npm run typecheckclean,npm run build(tsup) clean.0.3.0(from0.2.1): this changes the output shape for existing inputs relying on last-value-wins, so treating it as a minor bump rather than a patch protects consumers pinned via^0.2.x.