Skip to content

Commit 266e9f5

Browse files
author
Heath Morrison
committed
Continue _coerce after logical operators
1 parent 5091f71 commit 266e9f5

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/dao.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1559,7 +1559,8 @@ DataAccessObject._coerce = function(where) {
15591559
err.statusCode = 400;
15601560
throw err;
15611561
}
1562-
return where;
1562+
1563+
continue;
15631564
}
15641565
var DataType = props[p] && props[p].type;
15651566
if (!DataType) {

test/loopback-dl.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,16 @@ describe('DataAccessObject', function() {
14211421
assert.deepEqual(where, {or: [{age: 10}, {vip: true}]});
14221422
});
14231423

1424+
it('continues to coerce properties after a logical operator', function() {
1425+
var clause = {and: [{age: '10'}], vip: 'true'};
1426+
1427+
// Key order is predictable but not guaranteed. We prefer false negatives (failure) to false positives.
1428+
assert(Object.keys(clause)[0] === 'and', 'Unexpected key order.');
1429+
1430+
where = model._coerce(clause);
1431+
assert.deepEqual(where, {and: [{age: 10}], vip: true});
1432+
});
1433+
14241434
it('should throw if the where property is not an object', function() {
14251435
try {
14261436
// The where clause has to be an object
@@ -1473,6 +1483,20 @@ describe('DataAccessObject', function() {
14731483
assert(error, 'An error should have been thrown');
14741484
});
14751485

1486+
it('throws an error when malformed logical operators follow valid logical clauses', function() {
1487+
var invalid = {and: [{x: 1}], or: 'bogus'};
1488+
1489+
// Key order is predictable but not guaranteed. We prefer false negatives (failure) to false positives.
1490+
assert(Object.keys(invalid)[0] !== 'or', 'Unexpected key order.');
1491+
1492+
try {
1493+
model._coerce(invalid);
1494+
} catch (err) {
1495+
error = err;
1496+
}
1497+
assert(error, 'An error should have been thrown');
1498+
});
1499+
14761500
it('should throw if filter property is not an object', function() {
14771501
var filter = null;
14781502
try {

0 commit comments

Comments
 (0)