Merge pull request #947 from strongloop/update-errormsg
[SEMVER-MAJOR] Throw Error for property names with dots
This commit is contained in:
commit
e0a68289c5
|
@ -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.
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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,14 +390,12 @@ 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');
|
var Model = memory.createModel('DynamicDotted');
|
||||||
Model.create({ 'dot.name': 'dot.value' }, function(err) {
|
Model.create({ 'dot.name': 'dot.value' }, function(err) {
|
||||||
if (err) return done(err);
|
err.should.be.instanceOf(Error);
|
||||||
message.should.match(/Dotted.*dot\.name/);
|
err.message.should.match(/dot\(s\).*DynamicDotted.*dot\.name/);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue