Skip to content

Commit

Permalink
Added test and fix for STRICT null path queries. Fixed Date, Number, …
Browse files Browse the repository at this point in the history
…ObjectId, and String type to keep a null value as null in its `cast` function.
  • Loading branch information
bnoguchi committed Feb 14, 2011
1 parent c9f951d commit 65f3491
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/mongoose/schema/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ SchemaDate.prototype.checkRequired = function (value) {
*/

SchemaDate.prototype.cast = function (value) {
if (value === null) return value;
if (value instanceof Date)
return value;

Expand Down
1 change: 1 addition & 0 deletions lib/mongoose/schema/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ SchemaNumber.prototype.max = function (value, message) {

SchemaNumber.prototype.cast = function (value, doc) {
if (!isNaN(value)){
if (value === null) return value;
if ('string' === typeof value) value = Number(value);
if (value instanceof Number || typeof value == 'number' ||
(value.toString && value.toString() == Number(value)))
Expand Down
1 change: 1 addition & 0 deletions lib/mongoose/schema/objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ ObjectId.prototype.checkRequired = function(value){
*/

ObjectId.prototype.cast = function (value) {
if (value === null) return value;
if (value instanceof oid)
return value;
if (value.toString)
Expand Down
1 change: 1 addition & 0 deletions lib/mongoose/schema/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ SchemaString.prototype.checkRequired = function (v) {
*/

SchemaString.prototype.cast = function (value) {
if (value === null) return value;
if (value.toString) return value.toString();
throw new CastError('string', value);
};
Expand Down
18 changes: 18 additions & 0 deletions test/model.querying.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -840,5 +840,23 @@ module.exports = {
});
});
});
},

'test finding STRICT null matches': function () {
var db = start()
, BlogPostB = db.model('BlogPostB', collection + random());

BlogPostB.create({title: 'A', author: null}, function (err, createdA) {
should.strictEqual(err, null);
BlogPostB.create({title: 'B'}, function (err, createdB) {
should.strictEqual(err, null);
BlogPostB.find({author: {$in: [null], $exists: true}}, function (err, found) {
should.strictEqual(err, null);
found.should.have.length(1);
found[0]._id.should.eql(createdA._id);
db.close();
});
});
});
}
};

0 comments on commit 65f3491

Please sign in to comment.