Comments (15)
This is how parsing is supposed to be done AFAIK.
from qs.
@Marsup as in it should get set to 'false'
? Or the other way?
from qs.
If you have multiple parameters with the same name, it should become an array.
from qs.
Yep that is what happens currently. However some parsers would deduplicate this and just set it to the last defined value. For example parsing JSON does this if you define a property twice.
Would you be open to a pull request to add this functionality behind an option?
from qs.
I don't think parsers for other formats are relevant here, look at the other query string parsers, all the other ones I know do that.
I'll let @nlf decide on that behavior change but I think it'll be a no.
from qs.
Here is my use case if that helps justify the change:
I have an html form with a checkbox. Because browsers only pass the value of a checkbox if it is checked I also have a hidden input to set the unchecked value. This hidden input uses the same name as the checkbox input. When this data is then parsed by qs I get an array, not a single value which is what I would like.
from qs.
Bad design IMHO. If the value is missing, default to false.
from qs.
Not if you have to cater for true, false and undefined.
from qs.
Then preprocess the query you're sending, but not with some tricks with hidden fields. It's not the server side that needs fixing.
from qs.
Unfortunately I can't preprocess because we must cater for clients with no Javascript. Unfortunately the W3C spec forces developers down this road if they want to use checkboxes. If we could use Javascript I would just send JSON and not use query strings at all.
from qs.
Then why not radio buttons ? Tri-state checkbox in pure html don't exist, the only way to set it is through javascript, so no js, no tri-state. If your user has no possibility to set it himself, I don't see how this can make a difference between "false" and "never been touched false".
from qs.
That is true for a single form, but not for multiple with the same endpoint. When a user submits a form with no checkbox we receive undefined (which we interpret as no change) where as if we receive true or false we update the value given.
from qs.
Then what about not using the same name and default to it with joi (considering you're using it), like :
var schema = Joi.object().keys({
a: Joi.string().default(Joi.ref('adefault')),
adefault: Joi.string()
});
from qs.
I tend to agree that this shouldn't be qs
's problem, it providing the server with all passed in parameters is the idea. I don't really like the idea of adding in options for edge cases, especially where a little extra processing after the parsing is done is not much work in your app.
from qs.
Closing this unless there ends up being more demand for it
from qs.
Related Issues (20)
- Keeping nesting with custom decoder ? HOT 1
- qs.parse decoder option doesn't work on IOS web view environment HOT 1
- Unable to parse back a stringified object when too nested HOT 3
- `stringify` with `arrayFormat: 'comma'` does not encode commas in strings. HOT 13
- [spam]
- ESM & CJS - Built HOT 9
- Stringify null values HOT 9
- None. HOT 1
- Serialization for `Date` is not working when using `filter` option. HOT 3
- Issue using qs while using Express@5 HOT 7
- qs.stringify(json) and qs.parse(json) Results are inconsistent with expectations HOT 4
- Parsing the stringify result would lead to a different object HOT 1
- qs parse is not letting app load on production when imported from index.tsx HOT 1
- Feature request: export another endpoint that doesn't depend on function-bind and possibly other polyfills HOT 10
- Does not work in NextJS edge middleware HOT 3
- Stringify method with format: "RFC1738" does not encode parentheses -> ( ) HOT 3
- I'm appreciate with this library, however my new project use python. Are there any same packages in Python? HOT 1
- How can you keep the square brackets when using arrayFormat: comma HOT 4
- Add required key and type checking HOT 2
- qs.parse return different while the query is in different position HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from qs.