Merge pull request #1204 from schempy/fix/invalid-model-relation-name

Throw error when model relation name is trigger
This commit is contained in:
Miroslav Bajtoš 2017-01-03 10:40:13 +01:00 committed by GitHub
commit 66c54a9646
2 changed files with 28 additions and 0 deletions

View File

@ -461,6 +461,8 @@ DataSource.prototype.defineRelations = function(modelClass, relations) {
Object.keys(relations).forEach(function(rn) {
var r = relations[rn];
assert(DataSource.relationTypes.indexOf(r.type) !== -1, 'Invalid relation type: ' + r.type);
assert(isValidRelationName(rn), 'Invalid relation name: ' + rn);
var targetModel, polymorphicName;
if (r.polymorphic && r.type !== 'belongsTo' && !r.model) {
@ -503,6 +505,12 @@ DataSource.prototype.defineRelations = function(modelClass, relations) {
}
};
function isValidRelationName(relationName) {
var invalidRelationNames = ['trigger'];
return invalidRelationNames.indexOf(relationName) === -1;
}
/*!
* Set up the data access functions from the data source
* @param {Model} modelClass The model class

View File

@ -5609,4 +5609,24 @@ describe('relations', function() {
.catch(done);
});
});
describe('relation names', function() {
it('throws error when a relation name is `trigger`', function() {
Chapter = db.define('Chapter', {name: String});
(function() {
db.define(
'Book',
{name: String},
{
relations: {
trigger: {
model: 'Chapter',
type: 'hasMany',
},
},
});
}).should.throw('Invalid relation name: trigger');
});
});
});