#1261 Property name "constructor" is not allowed in 'Model' data (#1284)

* check if data has a constructor prop

* test data with no constructor

* test non function constructor

* test non function ctor message

* cleanup
This commit is contained in:
Thaer Abbas 2017-04-04 21:35:24 +04:00 committed by Sakib Hasan
parent 2e6fbadd9e
commit 57ead01624
2 changed files with 43 additions and 2 deletions

View File

@ -71,8 +71,8 @@ function ModelBaseClass(data, options) {
ModelBaseClass.prototype._initProperties = function(data, options) {
var self = this;
var ctor = this.constructor;
if (typeof data !== 'undefined' &&
// issue#1261
if (typeof data !== 'undefined' && data.constructor &&
typeof (data.constructor) !== 'function') {
throw new Error(g.f('Property name "{{constructor}}" is not allowed in %s data', ctor.modelName));
}

View File

@ -260,6 +260,47 @@ describe('ModelBuilder', function() {
follow.should.have.property('id');
assert.deepEqual(follow.id, {followerId: 1, followeeId: 2});
});
it('instantiates model from data with no constructor', function(done) {
var modelBuilder = new ModelBuilder();
var User = modelBuilder.define('User', {name: String, age: Number});
try {
var data = Object.create(null);
data.name = 'Joe';
data.age = 20;
var user = new User(data);
assert(true, 'The code is expected to pass');
} catch (e) {
assert(false, 'The code should have not thrown an error');
}
done();
});
it('instantiates model from data with non function constructor', function(done) {
var modelBuilder = new ModelBuilder();
var User = modelBuilder.define('User', {name: String, age: Number});
try {
var Person = function(name, age) {
this.name = name;
this.age = age;
};
Person.prototype.constructor = 'constructor';
var data = new Person('Joe', 20);
var user = new User(data);
assert(false, 'The code should have thrown an error');
} catch (e) {
e.message.should.equal('Property name "constructor" is not allowed in User data');
assert(true, 'The code is expected to throw an error');
}
done();
});
});
describe('DataSource ping', function() {