mongoose-unique-validator
Advanced tools
Comparing version 0.4.1 to 0.5.0
74
index.js
@@ -1,45 +0,39 @@ | ||
module.exports = function (schema, options) { | ||
var message = 'Error, expected `{PATH}` to be unique. Value: `{VALUE}`'; | ||
if (options && options.message) { | ||
message = options.message; | ||
} | ||
schema.eachPath(function (path, schemaType) { | ||
if (schemaTypeHasUniqueIndex(schemaType)) { | ||
var validator = buildUniqueValidator(path); | ||
message = buildMessage(schemaType.options.unique, message); | ||
schemaType.validate(validator, message); | ||
} | ||
}); | ||
}; | ||
'use strict'; | ||
function buildMessage(uniqueOption, message){ | ||
return typeof uniqueOption === 'string' ? uniqueOption : message; | ||
} | ||
// Export the mongoose plugin | ||
module.exports = function(schema, options) { | ||
options = options || {}; | ||
var message = options.message || 'Error, expected `{PATH}` to be unique. Value: `{VALUE}`'; | ||
function schemaTypeHasUniqueIndex(schemaType) { | ||
return schemaType._index && schemaType._index.unique; | ||
} | ||
schema.indexes().forEach(function(index) { | ||
var paths = Object.keys(index[0]); | ||
var indexOptions = index[1]; | ||
function buildUniqueValidator(path) { | ||
return function (value, respond) { | ||
var model = this.model(this.constructor.modelName); | ||
var query = buildQuery(path, value, this._id); | ||
var callback = buildValidationCallback(respond); | ||
model.findOne(query, callback); | ||
}; | ||
} | ||
if (indexOptions.unique) { | ||
paths.forEach(function(path) { | ||
var pathMessage = message; | ||
if (typeof indexOptions.unique === 'string') { | ||
pathMessage = indexOptions.unique; | ||
} | ||
function buildQuery(field, value, id) { | ||
var query = { $and: [] }; | ||
var target = {}; | ||
target[field] = value; | ||
query.$and.push({ _id: { $ne: id } }); | ||
query.$and.push(target); | ||
return query; | ||
} | ||
schema.path(path).validate(function(value, respond) { | ||
var doc = this; | ||
function buildValidationCallback(respond) { | ||
return function (err, document) { | ||
respond(!document); | ||
}; | ||
} | ||
var conditions = []; | ||
paths.forEach(function(path) { | ||
var condition = {}; | ||
condition[path] = doc[path]; | ||
conditions.push(condition); | ||
}); | ||
// Awaiting more official way to obtain reference to model. | ||
// https://github.com/Automattic/mongoose/issues/3430 | ||
var model = doc.model(doc.constructor.modelName); | ||
model.where({ $and: conditions }).count(function(err, count) { | ||
respond(((doc.isNew && count === 0) || (!doc.isNew && count === 1))); | ||
}); | ||
}, pathMessage); | ||
}); | ||
} | ||
}); | ||
}; |
{ | ||
"name": "mongoose-unique-validator", | ||
"version": "0.4.1", | ||
"description": "mongoose-unique-validator is a plugin which adds pre-save validation for unique fields within a Mongoose schema.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/jasmine-node spec/ --forceexit --captureExceptions" | ||
}, | ||
"keywords": [ | ||
"mongoose", | ||
"unique", | ||
"validator" | ||
], | ||
"author": { | ||
"name": "Blake Haswell", | ||
"email": "haswell00@gmail.com", | ||
"url": "http://blakehaswell.com/" | ||
}, | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/blakehaswell/mongoose-unique-validator.git" | ||
}, | ||
"devDependencies": { | ||
"jasmine-node": "1.14.x", | ||
"mongoose": "3.8.x" | ||
} | ||
"name": "mongoose-unique-validator", | ||
"version": "0.5.0", | ||
"description": "mongoose-unique-validator is a plugin which adds pre-save validation for unique fields within a Mongoose schema.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha test && ./node_modules/eslint/bin/eslint.js index.js test", | ||
"lint": "eslint index.js test" | ||
}, | ||
"keywords": [ | ||
"mongoose", | ||
"unique", | ||
"validator" | ||
], | ||
"author": { | ||
"name": "Blake Haswell", | ||
"email": "haswell00@gmail.com", | ||
"url": "http://blakehaswell.com/" | ||
}, | ||
"contributors": [{ | ||
"name": "Mike Botsko", | ||
"email": "botsko@gmail.com" | ||
}], | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/blakehaswell/mongoose-unique-validator.git" | ||
}, | ||
"devDependencies": { | ||
"bluebird": "^2.10.2", | ||
"chai": "^3.3.0", | ||
"eslint": "^1.6.0", | ||
"mocha": "^2.3.3" | ||
}, | ||
"peerDependencies": { | ||
"mongoose": "^4.1.10" | ||
} | ||
} |
mongoose-unique-validator | ||
========================= | ||
[![Build Status](https://travis-ci.org/blakehaswell/mongoose-unique-validator.svg)](https://travis-ci.org/blakehaswell/mongoose-unique-validator) | ||
mongoose-unique-validator is a plugin which adds pre-save validation for unique fields within a Mongoose schema. | ||
@@ -5,0 +7,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21178
12
338
89
1
4
2