lt, lte, gt & gte no more work with strings or mongodb ObjectIds
See original GitHub issueIn 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:
- Created 6 years ago
- Comments:9 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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?
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.