Skip to content

Commit

Permalink
Better error messages when unique validation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Oct 28, 2019
1 parent 5595e4c commit 3f43399
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
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
5 changes: 4 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,7 @@ 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 3f43399

Please sign in to comment.