Skip to content

Commit

Permalink
fix(model): throw for invalid include type (#10527)
Browse files Browse the repository at this point in the history
  • Loading branch information
sushantdhiman committed Mar 9, 2019
1 parent 0b5aa71 commit 454cf48
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 32 deletions.
66 changes: 37 additions & 29 deletions lib/model.js
Expand Up @@ -324,48 +324,56 @@ class Model {
}

static _conformInclude(include, self) {
let model;
if (include) {
let model;

if (include._pseudo) return include;
if (include._pseudo) return include;

include = this._transformStringAssociation(include, self);
include = this._transformStringAssociation(include, self);

if (include instanceof Association) {
if (self && include.target.name === self.name) {
model = include.source;
} else {
model = include.target;
if (include instanceof Association) {
if (self && include.target.name === self.name) {
model = include.source;
} else {
model = include.target;
}

return { model, association: include, as: include.as };
}

return { model, association: include, as: include.as };
}
if (include.prototype && include.prototype instanceof Model) {
return { model: include };
}
if (_.isPlainObject(include)) {
if (include.association) {
if (include.prototype && include.prototype instanceof Model) {
return { model: include };
}

include.association = this._transformStringAssociation(include.association, self);
if (_.isPlainObject(include)) {
if (include.association) {
include.association = this._transformStringAssociation(include.association, self);

if (self && include.association.target.name === self.name) {
model = include.association.source;
} else {
model = include.association.target;
if (self && include.association.target.name === self.name) {
model = include.association.source;
} else {
model = include.association.target;
}

if (!include.model) include.model = model;
if (!include.as) include.as = include.association.as;

this._conformOptions(include, model);
return include;
}

if (!include.model) {
include.model = model;
if (include.model) {
this._conformOptions(include, include.model);
return include;
}
if (!include.as) {
include.as = include.association.as;

if (include.all) {
this._conformOptions(include);
return include;
}
} else {
model = include.model;
}

this._conformOptions(include, model);
return include;
}

throw new Error('Include unexpected. Element has to be either a Model, an Association or an object.');
}

Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -67,7 +67,6 @@
"eslint": "^5.15.0",
"eslint-plugin-jsdoc": "^4.1.1",
"eslint-plugin-mocha": "^5.2.1",
"hints": "^1.x",
"husky": "^1.3.1",
"js-combinatorics": "^0.5.4",
"lcov-result-merger": "^3.0.0",
Expand Down
27 changes: 25 additions & 2 deletions test/unit/model/include.test.js
Expand Up @@ -8,7 +8,6 @@ const chai = require('chai'),

describe(Support.getTestDialectTeaser('Model'), () => {
describe('all', () => {

const Referral = current.define('referal');

Referral.belongsTo(Referral);
Expand Down Expand Up @@ -252,7 +251,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});

describe('_conformInclude: string alias', () => {
describe('_conformInclude', () => {
it('should expand association from string alias', function() {
const options = {
include: ['Owner']
Expand Down Expand Up @@ -282,6 +281,30 @@ describe(Support.getTestDialectTeaser('Model'), () => {
as: 'Owner'
});
});

it('should throw an error if invalid model is passed', function() {
const options = {
include: [{
model: null
}]
};

expect(() => {
Sequelize.Model._conformOptions(options, this.Company);
}).to.throw('Include unexpected. Element has to be either a Model, an Association or an object.');
});

it('should throw an error if invalid association is passed', function() {
const options = {
include: [{
association: null
}]
};

expect(() => {
Sequelize.Model._conformOptions(options, this.Company);
}).to.throw('Include unexpected. Element has to be either a Model, an Association or an object.');
});
});

describe('_getIncludedAssociation', () => {
Expand Down

0 comments on commit 454cf48

Please sign in to comment.