Merge pull request #470 from rudzon/master

Enhance updateAll to coerce the data per property definitions
This commit is contained in:
Raymond Feng 2015-02-24 08:28:39 -08:00
commit 6891da4535
2 changed files with 39 additions and 8 deletions

View File

@ -768,17 +768,28 @@ DataAccessObject._normalize = function (filter) {
};
function DateType(arg) {
return new Date(arg);
var d = new Date(arg);
if (isNaN(d.getTime())) {
throw new Error('Invalid date: ' + arg);
}
return d;
}
function BooleanType(val) {
if (val === 'true') {
return true;
} else if (val === 'false') {
return false;
} else {
return Boolean(val);
function BooleanType(arg) {
if (typeof arg === 'string') {
switch (arg) {
case 'true':
case '1':
return true;
case 'false':
case '0':
return false;
}
}
if (arg == null) {
return null;
}
return Boolean(arg);
}
function NumberType(val) {
@ -1530,6 +1541,8 @@ DataAccessObject.updateAll = function (where, data, options, cb) {
try {
where = removeUndefined(where);
where = Model._coerce(where);
data = removeUndefined(data);
data = Model._coerce(data);
} catch (err) {
return process.nextTick(function () {
cb(err);

View File

@ -654,6 +654,24 @@ describe('basic-querying', function () {
});
});
it('should ignore undefined values of data', function (done) {
User.update({name: 'John Lennon'}, {name: undefined}, function (err) {
should.not.exist(err);
User.find({where: {name: 'John Lennon'}}, function (err, data) {
should.not.exist(err);
data.length.should.equal(1);
done();
});
});
});
it('should coerce data', function (done) {
User.update({name: 'John Lennon'}, {birthday: 'invalidate'}, function (err) {
should.exist(err);
done();
});
});
});
});