Skip to content

Commit

Permalink
[service] remove dependency mongoose-beautiful-unique-validation beca…
Browse files Browse the repository at this point in the history
…use the name is too long (also it was causing a recursive stack overflow with mongoose 6.12)
  • Loading branch information
restjohn committed Aug 29, 2023
1 parent 124cb05 commit 0c960af
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 25 deletions.
12 changes: 0 additions & 12 deletions service/npm-shrinkwrap.json

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

1 change: 0 additions & 1 deletion service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"mime-types": "^2.1.35",
"moment": "2.22.2",
"mongoose": "^6.7.4",
"mongoose-beautiful-unique-validation": "7.1.1",
"multer": "^1.4.2",
"node-fetch": "^2.6.0",
"passport": "0.4.1",
Expand Down
11 changes: 10 additions & 1 deletion service/src/models/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,16 @@ EventSchema.virtual('id')
.get(function () { return this._id })
.set(function (x) { this._id = Number(x) })

EventSchema.plugin(require('mongoose-beautiful-unique-validation'));
async function uniqueViolationToValidationErrorHook(err, _, next) {
if (err.code === 11000 || err.code === 11001) {
err = new mongoose.Error.ValidationError()
err.errors = { name: { type: 'unique', message: 'Duplicate event name' }}
}
return next(err)
}
EventSchema.post('save', uniqueViolationToValidationErrorHook)
EventSchema.post('update', uniqueViolationToValidationErrorHook)
EventSchema.post('findOneAndUpdate', uniqueViolationToValidationErrorHook)

FormSchema.path('fields').validate(hasAtLeastOneField, 'A form must contain at least one field.');
FormSchema.path('fields').validate(fieldNamesAreUnique, 'Form field names must be unique.');
Expand Down
20 changes: 9 additions & 11 deletions service/test/event/eventCreateTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe("event create tests", function () {
.end(done);
});

it("should reject event with duplicate name", function (done) {
it("should reject event with duplicate name", async function() {
sinon.mock(TokenModel)
.expects('getToken')
.withArgs('12345')
Expand All @@ -212,7 +212,7 @@ describe("event create tests", function () {
.expects('insertOne')
.yields(uniqueError, null);

request(app)
const res = await request(app)
.post('/api/events')
.set('Accept', 'application/json')
.set('Authorization', 'Bearer 12345')
Expand All @@ -231,14 +231,12 @@ describe("event create tests", function () {
}]
}
})
.expect(400)
.expect(function (res) {
should.exist(res.body);
res.body.should.have.property('message').that.equals('Validation failed');
res.body.should.have.property('errors').that.is.an('object');
res.body.errors.should.have.property('name').that.is.an('object');
res.body.errors.name.should.have.property('message').that.equals('Event with name "Test" already exists.');
})
.end(done);

res.status.should.equal(400)
res.body.should.exist
res.body.message.should.equals('Validation failed');
res.body.should.have.property('errors').that.is.an('object');
res.body.errors.should.have.property('name').that.is.an('object');
res.body.errors.name.should.have.property('message').that.equals('Duplicate event name');
});
});

0 comments on commit 0c960af

Please sign in to comment.