Merge pull request #823 from strongloop/feature/handle-constructor
Prevent constructor to be property name
This commit is contained in:
commit
49c2bc214a
|
@ -257,6 +257,12 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
|
|||
deprecated('Property names containing a dot are not supported. ' +
|
||||
'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);
|
||||
|
|
|
@ -63,6 +63,11 @@ ModelBaseClass.prototype._initProperties = function (data, options) {
|
|||
var self = this;
|
||||
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) {
|
||||
// Convert the data to be plain object to avoid pollutions
|
||||
data = data.toObject(false);
|
||||
|
|
|
@ -379,6 +379,15 @@ describe('ModelDefinition class', function () {
|
|||
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) {
|
||||
var message = 'deprecation not reported';
|
||||
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() {
|
||||
var Model = memory.createModel('TwoArrays', {
|
||||
regular: Array,
|
||||
|
|
Loading…
Reference in New Issue