Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongoose-unique-validator

Package Overview
Dependencies
Maintainers
2
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-unique-validator - npm Package Compare versions

Comparing version 0.4.1 to 0.5.0

.editorconfig

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc