diff --git a/index.js b/index.js index f9c10dc..b2d2ba7 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ const get = require('lodash.get'); // Function typecheck helper const isFunc = (val) => typeof val === 'function'; -var deepPath = function(schema, pathName) { +const deepPath = function(schema, pathName) { let path; const paths = pathName.split('.'); @@ -29,8 +29,12 @@ module.exports = function(schema, options) { const type = options.type || 'unique'; const message = options.message || 'Error, expected `{PATH}` to be unique. Value: `{VALUE}`'; + // Mongoose Schema objects don't describe default _id indexes + // https://github.com/Automattic/mongoose/issues/5998 + const indexes = [[{ _id: 1 }, { unique: true }]].concat(schema.indexes()); + // Dynamically iterate all indexes - each(schema.indexes(), (index) => { + each(indexes, (index) => { const indexOptions = index[1]; if (indexOptions.unique) { diff --git a/package.json b/package.json index 5511c9b..1092f9d 100644 --- a/package.json +++ b/package.json @@ -33,10 +33,10 @@ "chai": "^4.1.2", "eslint": "^4.16.0", "mocha": "^5.0.0", - "mongoose": "^4.13.9" + "mongoose": "^5.0.0" }, "peerDependencies": { - "mongoose": "^4.9.0" + "mongoose": "^5.0.0" }, "dependencies": { "lodash.foreach": "^4.1.0", diff --git a/test/helpers.js b/test/helpers.js index fab460b..0fe234e 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -132,10 +132,6 @@ module.exports = { createCustomIdSchema: function() { return new mongoose.Schema({ - _id: { - type: String, - unique: true - }, position: Number }); }, diff --git a/test/tests/validation.spec.js b/test/tests/validation.spec.js index ac9e47d..fa3e254 100644 --- a/test/tests/validation.spec.js +++ b/test/tests/validation.spec.js @@ -235,16 +235,16 @@ module.exports = function(mongoose) { it('throws error on unique violation for custom _id field', function(done) { var Planet = mongoose.model('Planet', helpers.createCustomIdSchema().plugin(uniqueValidator)); + var id = new mongoose.Types.ObjectId('aporfghtyuqi'); // Save the first user - var promise = new Planet({ _id: 'mercury' }).save(); + var promise = new Planet({ _id: id }).save(); promise.then(function() { // Try saving a duplicate - new Planet({ _id: 'mercury' }).save().catch(function(err) { + new Planet({ _id: id }).save().catch(function(err) { expect(err.errors._id.name).to.equal('ValidatorError'); expect(err.errors._id.kind).to.equal('unique'); expect(err.errors._id.path).to.equal('_id'); - expect(err.errors._id.value).to.equal('mercury'); done(); }); @@ -255,7 +255,7 @@ module.exports = function(mongoose) { it('does not throw error when saving self (with custom _id field)', function(done) { var Planet = mongoose.model('Planet', helpers.createCustomIdSchema().plugin(uniqueValidator)); - var planet = new Planet({ _id: 'mercury' }); + var planet = new Planet({ _id: new mongoose.Types.ObjectId('aporfghtyuqi') }); // Save a user var promise = planet.save(); diff --git a/yarn.lock b/yarn.lock index 6ec0db8..939dc40 100644 --- a/yarn.lock +++ b/yarn.lock @@ -123,10 +123,6 @@ bson@~1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/bson/-/bson-1.0.4.tgz#93c10d39eaa5b58415cbc4052f3e53e562b0b72c" -buffer-shims@~1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/buffer-shims/-/buffer-shims-1.0.0.tgz#9978ce317388c649ad8793028c3477ef044a8b51" - caller-path@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/caller-path/-/caller-path-0.1.0.tgz#94085ef63581ecd3daa92444a8fe94e82577751f" @@ -270,10 +266,6 @@ doctrine@^2.1.0: dependencies: esutils "^2.0.2" -es6-promise@3.2.1: - version "3.2.1" - resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-3.2.1.tgz#ec56233868032909207170c39448e24449dd1fc4" - escape-string-regexp@1.0.5, escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" @@ -465,10 +457,6 @@ he@1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd" -hooks-fixed@2.0.2: - version "2.0.2" - resolved "https://registry.yarnpkg.com/hooks-fixed/-/hooks-fixed-2.0.2.tgz#20076daa07e77d8a6106883ce3f1722e051140b0" - iconv-lite@^0.4.17: version "0.4.19" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b" @@ -488,7 +476,7 @@ inflight@^1.0.4: once "^1.3.0" wrappy "1" -inherits@2, inherits@^2.0.3, inherits@~2.0.1, inherits@~2.0.3: +inherits@2, inherits@^2.0.3, inherits@~2.0.3: version "2.0.3" resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" @@ -582,9 +570,9 @@ jsonify@~0.0.0: version "0.0.0" resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73" -kareem@1.5.0: - version "1.5.0" - resolved "https://registry.yarnpkg.com/kareem/-/kareem-1.5.0.tgz#e3e4101d9dcfde299769daf4b4db64d895d17448" +kareem@2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/kareem/-/kareem-2.0.1.tgz#f17f77e9032f64aa402b334f91fb4407fe4c042c" levn@^0.3.0, levn@~0.3.0: version "0.3.0" @@ -647,36 +635,36 @@ mocha@^5.0.0: mkdirp "0.5.1" supports-color "4.4.0" -mongodb-core@2.1.18: - version "2.1.18" - resolved "https://registry.yarnpkg.com/mongodb-core/-/mongodb-core-2.1.18.tgz#4c46139bdf3a1f032ded91db49f38eec01659050" +mongodb-core@3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/mongodb-core/-/mongodb-core-3.0.1.tgz#ff6dc36ee96ff596953d80a6840d6731bc92efed" dependencies: bson "~1.0.4" - require_optional "~1.0.0" + require_optional "^1.0.1" -mongodb@2.2.34: - version "2.2.34" - resolved "https://registry.yarnpkg.com/mongodb/-/mongodb-2.2.34.tgz#a34f59bbeb61754aec432de72c3fe21526a44c1a" +mongodb@3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/mongodb/-/mongodb-3.0.1.tgz#278ee8006257ec22798594a6259546825d6de1b2" dependencies: - es6-promise "3.2.1" - mongodb-core "2.1.18" - readable-stream "2.2.7" + mongodb-core "3.0.1" + +mongoose-legacy-pluralize@1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.1.tgz#31ae25db45c30f1448c0f93f52769e903367c701" -mongoose@^4.13.9: - version "4.13.9" - resolved "https://registry.yarnpkg.com/mongoose/-/mongoose-4.13.9.tgz#ca4d99aed6e36e87854c2295387e7ea17966cfe3" +mongoose@^5.0.0: + version "5.0.1" + resolved "https://registry.yarnpkg.com/mongoose/-/mongoose-5.0.1.tgz#ded04311b9f13c8004e2b0097f3a61653a394361" dependencies: async "2.1.4" bson "~1.0.4" - hooks-fixed "2.0.2" - kareem "1.5.0" + kareem "2.0.1" lodash.get "4.4.2" - mongodb "2.2.34" + mongodb "3.0.1" + mongoose-legacy-pluralize "1.0.1" mpath "0.3.0" - mpromise "0.5.5" - mquery "2.3.3" + mquery "3.0.0-rc0" ms "2.0.0" - muri "1.3.0" regexp-clone "0.0.1" sliced "1.0.1" @@ -684,13 +672,9 @@ mpath@0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/mpath/-/mpath-0.3.0.tgz#7a58f789e9b5fd3c94520634157960f26bd5ef44" -mpromise@0.5.5: - version "0.5.5" - resolved "https://registry.yarnpkg.com/mpromise/-/mpromise-0.5.5.tgz#f5b24259d763acc2257b0a0c8c6d866fd51732e6" - -mquery@2.3.3: - version "2.3.3" - resolved "https://registry.yarnpkg.com/mquery/-/mquery-2.3.3.tgz#221412e5d4e7290ca5582dd16ea8f190a506b518" +mquery@3.0.0-rc0: + version "3.0.0-rc0" + resolved "https://registry.yarnpkg.com/mquery/-/mquery-3.0.0-rc0.tgz#05ec656e92f079828bedf4202e60fb8eaacb9f47" dependencies: bluebird "3.5.0" debug "2.6.9" @@ -701,10 +685,6 @@ ms@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" -muri@1.3.0: - version "1.3.0" - resolved "https://registry.yarnpkg.com/muri/-/muri-1.3.0.tgz#aeccf3db64c56aa7c5b34e00f95b7878527a4721" - mute-stream@0.0.7: version "0.0.7" resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab" @@ -790,18 +770,6 @@ pseudomap@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3" -readable-stream@2.2.7: - version "2.2.7" - resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.2.7.tgz#07057acbe2467b22042d36f98c5ad507054e95b1" - dependencies: - buffer-shims "~1.0.0" - core-util-is "~1.0.0" - inherits "~2.0.1" - isarray "~1.0.0" - process-nextick-args "~1.0.6" - string_decoder "~1.0.0" - util-deprecate "~1.0.1" - readable-stream@^2.2.2: version "2.3.3" resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.3.tgz#368f2512d79f9d46fdfc71349ae7878bbc1eb95c" @@ -825,7 +793,7 @@ require-uncached@^1.0.3: caller-path "^0.1.0" resolve-from "^1.0.0" -require_optional@~1.0.0: +require_optional@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/require_optional/-/require_optional-1.0.1.tgz#4cf35a4247f64ca3df8c2ef208cc494b1ca8fc2e" dependencies: @@ -914,7 +882,7 @@ string-width@^2.0.0, string-width@^2.1.0: is-fullwidth-code-point "^2.0.0" strip-ansi "^4.0.0" -string_decoder@~1.0.0, string_decoder@~1.0.3: +string_decoder@~1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.0.3.tgz#0fc67d7c141825de94282dd536bec6b9bce860ab" dependencies: