Merge pull request #947 from strongloop/update-errormsg

[SEMVER-MAJOR] Throw Error for property names with dots
This commit is contained in:
Gunjan Pandya 2016-06-24 16:50:00 -04:00 committed by GitHub
commit e0a68289c5
4 changed files with 28 additions and 24 deletions

View File

@ -75,3 +75,11 @@ Model events
See [related code change](https://github.com/strongloop/loopback-datasource-juggler/pull/965) See [related code change](https://github.com/strongloop/loopback-datasource-juggler/pull/965)
for more details. for more details.
## Model property names with dot(s) generates an error
Instead of showing a deprecation warning for model property names with dots,
for example, `customer.name: 'string'`, an error is thrown in 3.0 for
enforcing use of valid model property names.
See [related code change](https://github.com/strongloop/loopback-datasource-juggler/pull/947) for more details.

View File

@ -246,9 +246,9 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
delete properties[p]; delete properties[p];
} }
// Warn about properties with unsupported names // Throw error for properties with unsupported names
if (/\./.test(p)) { if (/\./.test(p)) {
deprecated('Property names containing a dot are not supported. ' + throw new Error('Property names containing dot(s) are not supported. ' +
'Model: ' + className + ', property: ' + p); 'Model: ' + className + ', property: ' + p);
} }

View File

@ -232,11 +232,12 @@ ModelBaseClass.prototype._initProperties = function(data, options) {
self[p] = self.__data[p] = self[p] = self.__data[p] =
(propVal !== undefined) ? propVal : self.__cachedRelations[p]; (propVal !== undefined) ? propVal : self.__cachedRelations[p];
// Warn about properties with unsupported names // Throw error for properties with unsupported names
if (/\./.test(p)) { if (/\./.test(p)) {
deprecated('Property names containing a dot are not supported. ' + throw new Error(
'Model: ' + this.constructor.modelName + 'Property names containing dot(s) are not supported. ' +
', dynamic property: ' + p); 'Model: ' + this.constructor.modelName + ', dynamic property: ' +
p);
} }
} else if (strict === 'throw') { } else if (strict === 'throw') {
throw new Error('Unknown property: ' + p); throw new Error('Unknown property: ' + p);

View File

@ -375,13 +375,10 @@ describe('ModelDefinition class', function() {
}); });
}); });
it('should report deprecation warning for property names containing dot', function() { it('should throw error for property names containing dot', function() {
var message = 'deprecation not reported'; (function() { memory.createModel('Dotted', { 'dot.name': String }); })
process.once('deprecation', function(err) { message = err.message; }); .should
.throw(/dot\(s\).*Dotted.*dot\.name/);
memory.createModel('Dotted', { 'dot.name': String });
message.should.match(/Dotted.*dot\.name/);
}); });
it('should report deprecation warning for property named constructor', function() { it('should report deprecation warning for property named constructor', function() {
@ -393,17 +390,15 @@ describe('ModelDefinition class', function() {
message.should.match(/Property name should not be "constructor" in Model: Ctor/); 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 throw error for dynamic property names containing dot',
var message = 'deprecation not reported'; function(done) {
process.once('deprecation', function(err) { message = err.message; }); var Model = memory.createModel('DynamicDotted');
Model.create({ 'dot.name': 'dot.value' }, function(err) {
var Model = memory.createModel('DynamicDotted'); err.should.be.instanceOf(Error);
Model.create({ 'dot.name': 'dot.value' }, function(err) { err.message.should.match(/dot\(s\).*DynamicDotted.*dot\.name/);
if (err) return done(err); done();
message.should.match(/Dotted.*dot\.name/); });
done();
}); });
});
it('should throw error for dynamic property named constructor', function(done) { it('should throw error for dynamic property named constructor', function(done) {
var Model = memory.createModel('DynamicCtor'); var Model = memory.createModel('DynamicCtor');