mongoose-unique-validator
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -1,6 +0,5 @@ | ||
module.exports = function (schema, options) { | ||
var mongoose = options.mongoose; | ||
module.exports = function (schema) { | ||
schema.eachPath(function (path, schemaType) { | ||
if (schemaTypeHasUniqueIndex(schemaType)) { | ||
var validator = buildUniqueValidator(path, mongoose); | ||
var validator = buildUniqueValidator(path); | ||
schemaType.validate(validator, 'unique'); | ||
@@ -15,5 +14,5 @@ } | ||
function buildUniqueValidator(path, mongoose) { | ||
function buildUniqueValidator(path) { | ||
return function (value, respond) { | ||
var model = mongoose.connection.model(this.constructor.modelName); | ||
var model = this.model(this.constructor.modelName); | ||
var query = buildQuery(path, value, this._id); | ||
@@ -20,0 +19,0 @@ var callback = buildValidationCallback(respond); |
{ | ||
"name": "mongoose-unique-validator", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "mongoose-unique-validator is a plugin which adds pre-save validation for unique fields within a Mongoose schema.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -17,3 +17,3 @@ mongoose-unique-validator | ||
Simply apply the plugin to your schema and pass in your `mongoose` instance: | ||
Then, simply apply the plugin to your schema: | ||
@@ -25,3 +25,3 @@ ```js | ||
var mySchema = mongoose.Schema(/* put your schema definition here */); | ||
mySchema.plugin(uniqueValidator, { mongoose: mongoose }); | ||
mySchema.plugin(uniqueValidator); | ||
``` | ||
@@ -47,3 +47,3 @@ | ||
// Apply the uniqueValidator plugin to userSchema. | ||
userSchema.plugin(uniqueValidator, { mongoose: mongoose }); | ||
userSchema.plugin(uniqueValidator); | ||
``` | ||
@@ -50,0 +50,0 @@ |
@@ -22,3 +22,3 @@ // TODO Fix callback hell with Q promise library. | ||
}); | ||
userSchema.plugin(uniqueValidator, { mongoose: mongoose }); | ||
userSchema.plugin(uniqueValidator); | ||
var User = mongoose.model('User', userSchema); | ||
@@ -25,0 +25,0 @@ |
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
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
7927
135