Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add allowEmptyArrays option #238

Closed
wants to merge 1 commit into from

Conversation

stevenmiller888
Copy link

@stevenmiller888 stevenmiller888 commented Feb 4, 2020

This is one possible solution to #231. Solves it by adding an allowEmptyArrays option, which will do the following, for example:

stringify: { a: [] } => 'a[]=' (instead of '')
parse: 'a[]=' => { a: [] } (instead of { a: [null] }).

If we like the idea, I'm happy to clean it up and add docs.

@sindresorhus
Copy link
Owner

This would indeed solve this exact problem, but there are many other problems with arrays. I think it would be better to solve it for real by letting to explicitly specify that it's an array. Then there's no possible ambiguity. See: #210 (comment) Thoughts on that?

// @mitar

@mitar
Copy link

mitar commented Feb 20, 2020

@stevenmiller888 I like it, but how do we encode null then now?

@mjsarfatti
Copy link

This has been causing unexpected results and bugs in my applications. Bugs extremely hard to detect, especially since this library is used by other libraries in turn.

Just spent half a day pinpointing the issues, and I finally realised the problem is in query-string.

If in a form you have a set of checkboxes, you can send it to the server as an array of params.
Now, if the user deselects all the checkboxes, how do I tell the server that the selection is now empty? I would assume that if I send a param as an array, empty or not, this param will reach the server.

In my opinion this shouldn't even be behind a flag, but it should be standard behaviour. Think about it. If you were writing an application that can send an array of values to the server, would you ever think that sending an empty array would have no effect and would actually send nothing?

If making it standard we are breaking past behaviours, maybe it should be a new major release. If that is not possible at the moment, would you be able to release it behind the flag as @stevenmiller888 suggested? This has been open for some time now.

This is just my opinion anyway, all my gratitude for the work you put towards this package 🙏

@DV8FromTheWorld
Copy link
Contributor

DV8FromTheWorld commented Sep 25, 2020

Instead of a flag to allow empty arrays in other formats, it might be better to introduce a format that is more explicit.
Check out the proposal for arrayFormat: bracket-separator and the question in regard to handling empty arrays

Base automatically changed from master to main January 24, 2021 06:08
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.

5 participants