Giter Site home page Giter Site logo

Comments (15)

Marsup avatar Marsup commented on April 27, 2024

This is how parsing is supposed to be done AFAIK.

from qs.

RGBboy avatar RGBboy commented on April 27, 2024

@Marsup as in it should get set to 'false'? Or the other way?

from qs.

Marsup avatar Marsup commented on April 27, 2024

If you have multiple parameters with the same name, it should become an array.

from qs.

RGBboy avatar RGBboy commented on April 27, 2024

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.

Marsup avatar Marsup commented on April 27, 2024

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.

RGBboy avatar RGBboy commented on April 27, 2024

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.

Marsup avatar Marsup commented on April 27, 2024

Bad design IMHO. If the value is missing, default to false.

from qs.

RGBboy avatar RGBboy commented on April 27, 2024

Not if you have to cater for true, false and undefined.

from qs.

Marsup avatar Marsup commented on April 27, 2024

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.

RGBboy avatar RGBboy commented on April 27, 2024

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.

Marsup avatar Marsup commented on April 27, 2024

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.

RGBboy avatar RGBboy commented on April 27, 2024

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.

Marsup avatar Marsup commented on April 27, 2024

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.

nlf avatar nlf commented on April 27, 2024

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.

nlf avatar nlf commented on April 27, 2024

Closing this unless there ends up being more demand for it

from qs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.