Merge pull request #823 from strongloop/feature/handle-constructor

Prevent constructor to be property name
This commit is contained in:
Janny 2016-02-02 13:29:03 -05:00
commit 49c2bc214a
3 changed files with 28 additions and 0 deletions

View File

@ -257,6 +257,12 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
deprecated('Property names containing a dot are not supported. ' + deprecated('Property names containing a dot are not supported. ' +
'Model: ' + className + ', property: ' + p); 'Model: ' + className + ', property: ' + p);
} }
// Warn if property name is 'constructor'
if (p === "constructor") {
deprecated('Property name should not be "constructor" in Model: ' +
className);
}
} }
var modelDefinition = new ModelDefinition(this, className, properties, settings); var modelDefinition = new ModelDefinition(this, className, properties, settings);

View File

@ -63,6 +63,11 @@ ModelBaseClass.prototype._initProperties = function (data, options) {
var self = this; var self = this;
var ctor = this.constructor; var ctor = this.constructor;
if (typeof data !== 'undefined' &&
typeof (data.constructor) !== 'function') {
throw new Error('Property name "constructor" is not allowed in ' + ctor.modelName +' data');
}
if(data instanceof ctor) { if(data instanceof ctor) {
// Convert the data to be plain object to avoid pollutions // Convert the data to be plain object to avoid pollutions
data = data.toObject(false); data = data.toObject(false);

View File

@ -379,6 +379,15 @@ describe('ModelDefinition class', function () {
message.should.match(/Dotted.*dot\.name/); message.should.match(/Dotted.*dot\.name/);
}); });
it('should report deprecation warning for property named constructor', function() {
var message = 'deprecation not reported';
process.once('deprecation', function(err) { message = err.message; });
memory.createModel('Ctor', { 'constructor': String });
message.should.match(/Property name should not be "constructor" in Model: Ctor/);
});
it('should report deprecation warning for dynamic property names containing dot', function(done) { it('should report deprecation warning for dynamic property names containing dot', function(done) {
var message = 'deprecation not reported'; var message = 'deprecation not reported';
process.once('deprecation', function(err) { message = err.message; }); process.once('deprecation', function(err) { message = err.message; });
@ -391,6 +400,14 @@ describe('ModelDefinition class', function () {
}); });
}); });
it('should throw error for dynamic property named constructor', function(done) {
var Model = memory.createModel('DynamicCtor');
Model.create({ 'constructor': 'myCtor' }, function(err) {
assert.equal(err.message, 'Property name "constructor" is not allowed in DynamicCtor data');
done();
});
});
it('should support "array" type shortcut', function() { it('should support "array" type shortcut', function() {
var Model = memory.createModel('TwoArrays', { var Model = memory.createModel('TwoArrays', {
regular: Array, regular: Array,