Take strict: validate and throw settings into account
This commit is contained in:
parent
8b06a9d39d
commit
c7caa0a7c1
18
lib/dao.js
18
lib/dao.js
|
@ -2336,9 +2336,21 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, op
|
||||||
data = ctx.data;
|
data = ctx.data;
|
||||||
|
|
||||||
if (strict) {
|
if (strict) {
|
||||||
var keys = Object.keys(Model.definition.properties);
|
var props = Model.definition.properties;
|
||||||
data = utils.selectFields(keys)(data);
|
var keys = Object.keys(data);
|
||||||
data = removeUndefined(data);
|
var result = {};
|
||||||
|
for (var i = 0; i < keys.length; i++) {
|
||||||
|
key = keys[i];
|
||||||
|
if (props[key]) {
|
||||||
|
result[key] = data[key];
|
||||||
|
} else if (strict === 'throw') {
|
||||||
|
cb(new Error('Unknown property: ' + key));
|
||||||
|
return;
|
||||||
|
} else if (strict === 'validate') {
|
||||||
|
inst.__unknownProperties.push(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
data = removeUndefined(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
// update instance's properties
|
// update instance's properties
|
||||||
|
|
|
@ -491,7 +491,7 @@ describe('manipulation', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should ignore unknown attributes', function(done) {
|
it('should ignore unknown attributes when strict: true', function(done) {
|
||||||
person.updateAttributes({foo:'bar'},
|
person.updateAttributes({foo:'bar'},
|
||||||
function(err, p) {
|
function(err, p) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
|
@ -503,6 +503,41 @@ describe('manipulation', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should throw error on unknown attributes when strict: throw', function(done) {
|
||||||
|
Person.definition.settings.strict = 'throw';
|
||||||
|
Person.findById(person.id, function(err, p) {
|
||||||
|
p.updateAttributes({foo:'bar'},
|
||||||
|
function(err, p) {
|
||||||
|
should.exist(err);
|
||||||
|
err.name.should.equal('Error');
|
||||||
|
err.message.should.equal('Unknown property: foo');
|
||||||
|
should.not.exist(p);
|
||||||
|
Person.findById(person.id, function(e, p) {
|
||||||
|
if (e) return done(e);
|
||||||
|
should.not.exist(p.foo);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw error on unknown attributes when strict: throw', function(done) {
|
||||||
|
Person.definition.settings.strict = 'validate';
|
||||||
|
Person.findById(person.id, function(err, p) {
|
||||||
|
p.updateAttributes({foo:'bar'},
|
||||||
|
function(err, p) {
|
||||||
|
should.exist(err);
|
||||||
|
err.name.should.equal('ValidationError');
|
||||||
|
err.message.should.containEql('`foo` is not defined in the model');
|
||||||
|
Person.findById(person.id, function(e, p) {
|
||||||
|
if (e) return done(e);
|
||||||
|
should.not.exist(p.foo);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should allow same id value on updateAttributes', function(done) {
|
it('should allow same id value on updateAttributes', function(done) {
|
||||||
person.updateAttributes({id: person.id, name: 'John'},
|
person.updateAttributes({id: person.id, name: 'John'},
|
||||||
|
|
Loading…
Reference in New Issue