Turistforeningen / node-mongo-querystring

Query builder for accepting URL query parameters into your MongoDB queries. Safe and feature rich. Supports most of MongoDB's query operators such as $eq, $gt, $lt, $ne, $in, $nin, $exists, $regex, geospatial queries such as bbox and near, as well as your own custom query business logic!
MIT License
100 stars 31 forks source link

Why discard Booleans in parse? #78

Open edgracilla opened 4 years ago

edgracilla commented 4 years ago

The following test will fail, since it totally disallow the non-string values (stated in test.js line 520)

it('return boolean as boolean', () => {
  query = mqs.parse({
    foo: true,
    bar: false,
  });
  assert.deepEqual(query, {
    foo: true,
    bar: false,
  });
});

But here is my use case, I'm using fastify and utilized their integrated ajv as my validator. My ajv was configured to do a type coercion, hence, if I pass the following query ?foo=true&bar=false the returned value from validator are boolean values.

{
  foo: true,
  bar: false
}

If I pass this to mqs.parse() it returns an empty object since mqs totally disallow bool values.

Turning off the coercion is not an option for me. And converting it back to string is a dirty hack. I guess it might be good if this will allow by mqs itself.

I can send a PR and test, but I want to know the importance of discarding the boolean values in test.js line 520, if there's any.