Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
mongoose-unique-validator
Advanced tools
mongoose-unique-validator is a plugin which adds pre-save validation for unique fields within a Mongoose schema.
mongoose-unique-validator is a plugin which adds pre-save validation for unique fields within a Mongoose schema.
This makes error handling much easier, since you will get a Mongoose validation error when you attempt to violate a unique constraint, rather than an E11000 error from MongoDB.
npm install mongoose-unique-validator
Simply apply the plugin to your schema and pass in your mongoose
instance:
var mongoose = require('mongoose');
var uniqueValidator = require('mongoose-unique-validator');
var mySchema = mongoose.Schema(/* put your schema definition here */);
mySchema.plugin(uniqueValidator, { mongoose: mongoose });
Let’s say you have a user schema. You can easily add validation for the unique constraints in this schema by applying
the uniqueValidator
plugin to your user schema:
var mongoose = require('mongoose');
var uniqueValidator = require('mongoose-unique-validator');
// Define your schema as normal.
var userSchema = mongoose.Schema({
username: { type: String, required: true, unique: true },
email: { type: String, index: true, unique: true, required: true },
password: { type: String, required: true }
});
// Apply the uniqueValidator plugin to userSchema.
userSchema.plugin(uniqueValidator, { mongoose: mongoose });
Now when you try to save a user, the unique validator will check for duplicate database entries and report them just like any other validation error:
var user = new User({ username: 'JohnSmith', email: 'john.smith@gmail.com', password: 'j0hnNYb0i' });
user.save(function (err) {
console.log(err);
});
{
message: 'Validation failed',
name: 'ValidationError',
errors: {
username: {
message: 'Validator "unique" failed for path username with value `JohnSmith`',
name: 'ValidatorError',
path: 'username',
type: 'unique',
value: 'JohnSmith'
}
}
}
FAQs
mongoose-unique-validator is a plugin which adds pre-save validation for unique fields within a Mongoose schema.
We found that mongoose-unique-validator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.