Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error messages when unique validation fails (mongodb) #1857

Merged
merged 4 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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