Do not coerce RegExp objects to strings

Queries like `{where: {name: /John.*/i}}` should work. Notice there is no
`regexp` operator (ie. `{where: {name: {regexp: /John.*/i}}}`).
This commit is contained in:
Simon Ho 2015-08-12 17:59:55 -07:00
parent 62624203d1
commit e7e074f97b
2 changed files with 15 additions and 2 deletions

View File

@ -1196,8 +1196,11 @@ DataAccessObject._coerce = function (where) {
} }
} else { } else {
if (val != null) { if (val != null) {
if (operator === 'regexp' && val instanceof RegExp) { if (operator === null && val instanceof RegExp) {
// do not coerce regex literals/objects // Normalize {name: /A/} to {name: {regexp: /A/}}
operator = 'regexp';
} else if (operator === 'regexp' && val instanceof RegExp) {
// Do not coerce regex literals/objects
} else if (!((operator === 'like' || operator === 'nlike') && val instanceof RegExp)) { } else if (!((operator === 'like' || operator === 'nlike') && val instanceof RegExp)) {
val = DataType(val); val = DataType(val);
} }

View File

@ -356,6 +356,16 @@ describe('Memory connector', function() {
}); });
}); });
it('should work when a regex is provided without the regexp operator',
function(done) {
User.find({where: {name: /John.*/i}}, function(err, users) {
should.not.exist(err);
users.length.should.equal(1);
users[0].name.should.equal('John Lennon');
done();
});
});
it('should support the regexp operator with regex strings', function(done) { it('should support the regexp operator with regex strings', function(done) {
User.find({where: {name: {regexp: '^J'}}}, function(err, users) { User.find({where: {name: {regexp: '^J'}}}, function(err, users) {
should.not.exist(err); should.not.exist(err);