question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

lt, lte, gt & gte no more work with strings or mongodb ObjectIds

See original GitHub issue

In previous versions of chaiJS expect(x).to.be.lt(y) used to work with x and y being strings or mongodb ids.

Since recently, this comparison raises AssertionError: expected 'x' to be a number or a date .

This is a serious limitation, especially when checking the sort order of data arrays returned by RESTful APIs.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
meebercommented, Jul 27, 2017

I’m definitely in favor of allowing strings as long as the target and given value are both strings.

The mongo objectids part is a bit tougher. I assume they’re objects that get coerced to strings during comparison via .valueOf?

1reaction
meebercommented, Jul 27, 2017

In general I’m opposed to type coercion in an assertion library. However, in this particular case I think the normal risks associated with type coercion are mitigated.

The main mitigation is that both the target and the given value must both be strings; the same coercion happens to both values using the same string-to-number algorithm. This eliminates the element of surprise that is usually associated with problems that arise from type coercion. To me, the fact that strings are being converted to unicode code points in the background isn’t significantly different than dates being converted to milliseconds since the Unix epoch. I’m having trouble thinking of what exactly we’re protecting the user against by disallowing string-to-string comparisons.

I am, however, opposed to object-to-object comparisons; the coercion algorithm in that case isn’t consistent, and a lot of surprising things could happen.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues with $gte and $lte for string comparision - MongoDB
I am trying to get all documents whose last name less than or equal to C. But it does not bring data starting...
Read more >
MongoDB $gt/$lt operators with prices stored as strings
I'm trying to query my database for prices greater than/less than a user specified number. In my ...
Read more >
$lte and $gt Query Operators not working properly for String ...
2. Query for documents using $lt Query Operator to get all the documents having "name" starting with A,B and C. > db.people.find({"name":{" ...
Read more >
MongoDB Comparison Query Operators - $gt, $lt, $gte, $lte
Here, is the collection bellow. Sample collection "testtable". { "_id" : ObjectId("528f34950fe5e6467e58ae77"), "user_id" : "user1", " ...
Read more >
query-params-mongo - npm
Converts HTTP URL query string parameters to MongoDB criteria, consisting of filter, ... eq, ne, gt, gte, lt, lte, in, nin, all, exists....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found