Merge pull request #1204 from schempy/fix/invalid-model-relation-name
Throw error when model relation name is trigger
This commit is contained in:
commit
66c54a9646
|
@ -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
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue