Skip to content

Commit

Permalink
Better error messages when unique validation fails (mongodb) (#1857)
Browse files Browse the repository at this point in the history
* Better error messages when unique validation fails

* Updated tests

* Switch to Jest reporters API

Fixes this warning:
testResultsProcessor support is deprecated. Please use jest reporter. See https://github.com/jest-community/jest-junit#usage:
  • Loading branch information
Vultraz authored and MadeByMike committed Oct 31, 2019
1 parent 7abfd0f commit ba8aef7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-wolves-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/adapter-mongoose': patch
---

Deployed mongoose-unique-validator for a more readable error message when unique checks fail
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
- run: yarn lint:markdown
- run:
name: Unit tests
command: yarn jest --ci --testResultsProcessor="jest-junit" --maxWorkers=1
command: yarn jest --ci --reporters=default --reporters=jest-junit --maxWorkers=1
environment:
JEST_JUNIT_OUTPUT: 'reports/junit/js-test-results.xml'
- store_test_results:
Expand Down
4 changes: 2 additions & 2 deletions api-tests/uniqueness/unique.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ multiAdapterRunners().map(({ runner, adapterName }) =>
});

expect(errors).toHaveProperty('0.message');
expect(errors[0].message).toEqual(expect.stringMatching(/duplicate key/));
expect(errors[0].message).toEqual(expect.stringMatching(/duplicate key|to be unique/));
})
);

Expand All @@ -59,7 +59,7 @@ multiAdapterRunners().map(({ runner, adapterName }) =>
});

expect(errors).toHaveProperty('0.message');
expect(errors[0].message).toEqual(expect.stringMatching(/duplicate key/));
expect(errors[0].message).toEqual(expect.stringMatching(/duplicate key|to be unique/));
})
);

Expand Down
9 changes: 8 additions & 1 deletion packages/adapter-mongoose/lib/adapter-mongoose.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');

const pSettle = require('p-settle');
const {
escapeRegExp,
Expand Down Expand Up @@ -28,6 +30,7 @@ class MongooseAdapter extends BaseKeystoneAdapter {
if (debugMongoose()) {
this.mongoose.set('debug', true);
}
this.mongoose.plugin(uniqueValidator);
this.listAdapterClass = this.listAdapterClass || this.defaultListAdapterClass;
}

Expand Down Expand Up @@ -184,7 +187,11 @@ class MongooseListAdapter extends BaseListAdapter {
_update(id, data) {
// Avoid any kind of injection attack by explicitly doing a `$set` operation
// Return the modified item, not the original
return this.model.findByIdAndUpdate(id, { $set: data }, { new: true });
return this.model.findByIdAndUpdate(
id,
{ $set: data },
{ new: true, runValidators: true, context: 'query' }
);
}

_findAll() {
Expand Down
1 change: 1 addition & 0 deletions packages/adapter-mongoose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@sindresorhus/slugify": "^0.6.0",
"lodash.omitby": "^4.6.0",
"mongoose": "^5.7.7",
"mongoose-unique-validator": "^2.0.3",
"p-settle": "^3.1.0",
"pluralize": "^7.0.0"
}
Expand Down

0 comments on commit ba8aef7

Please sign in to comment.