Skip to content

Commit

Permalink
Fix: mongoose schema instance error fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Devorein committed Jul 18, 2020
1 parent 2058a25 commit 2099d7e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 36 deletions.
6 changes: 6 additions & 0 deletions .huskyrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
hooks: {
'pre-commit': 'lint-staged && npm run test',
'pre-push': 'npm run lint && npm run test'
}
};
24 changes: 0 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,16 @@
"mongoose": "^5.9.25",
"pluralize": "^8.0.0",
"string": "^3.3.3",
"typescript": "^3.9.6"
"typescript": "^3.9.7"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
"dotenv": "^8.2.0",
"env-cmd": "^10.1.0",
"eslint": "^7.4.0",
"eslint-plugin-jest": "^23.18.0",
"eslint-plugin-prettier": "^3.1.4",
"jest": "^26.1.0",
"nodemon": "^2.0.4"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged && npm run test",
"pre-push": "npm run lint && npm run test"
}
},
"lint-staged": {
"*.js": [
"npm run lint"
Expand Down
5 changes: 2 additions & 3 deletions src/generateTypeTypedefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ module.exports = function (baseSchema, Validators) {

function generateVariant (value, isArray) {
let variant = 'scalar';
const instanceOfSchema = value instanceof mongoose.Schema;
const instanceOfSchema = value instanceof mongoose.Schema || Object.getPrototypeOf(value).instanceOfSchema;
if (instanceOfSchema) variant = 'type';
else if (value.enum) variant = 'enum';
else if (value.ref) variant = 'ref';
Expand Down Expand Up @@ -186,7 +186,6 @@ module.exports = function (baseSchema, Validators) {
function parseSchema (schema, parentKey, path = undefined) {
Object.entries(schema.obj).forEach(([ key, value ]) => {
const isArray = Array.isArray(value);
const instanceOfSchema = (isArray ? value[0] : value) instanceof mongoose.Schema;
value = isArray ? value[0] : value;
const extractedFieldOptions = extractFieldOptions(value, parentKey, isArray);
const variant = generateVariant(value, isArray);
Expand Down Expand Up @@ -215,7 +214,7 @@ module.exports = function (baseSchema, Validators) {
if (!inputs[input_key]) inputs[input_key] = {};
if (writable) inputs[input_key][key] = { value: transformed_input_type, variant };

fields[key] = !instanceOfSchema ? value : 'Schema';
fields[key] = !variant.match(/(type)/) ? value : 'Schema';

if (variant.match(/(type)/)) parseSchema(value, field_type, `${path ? path + '.' : ''}${key}`);
});
Expand Down

0 comments on commit 2099d7e

Please sign in to comment.