📜  mongoose-unique-validator - Javascript (1)

📅  最后修改于: 2023-12-03 15:03:02.503000             🧑  作者: Mango

Mongoose-unique-validator

Mongoose-unique-validator is a plugin for Mongoose that adds a pre-save validation for unique fields. This plugin simplifies the process of validating unique fields for MongoDB documents.

Installation

To use Mongoose-unique-validator, you need to have both Mongoose and Mongoose-unique-validator installed in your project. You can install both via NPM:

npm install mongoose mongoose-unique-validator
Usage

To use Mongoose-unique-validator, you need to include it as a plugin in your Mongoose schema. Here's an example:

const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');

const userSchema = new mongoose.Schema({
  username: {
    type: String,
    required: true,
    unique: true,
  },
  email: {
    type: String,
    required: true,
    unique: true,
  },
});

userSchema.plugin(uniqueValidator);

const User = mongoose.model('User', userSchema);

module.exports = User;

In this example, we define a Mongoose schema for a user. The username and email fields are defined as unique, using the unique option. We then add the Mongoose-unique-validator plugin to the schema using the plugin() method.

Now, when we try to save a user document, Mongoose-unique-validator will validate that the username and email fields are unique before actually saving the document.

If the validation fails, Mongoose-unique-validator will throw a validation error that you can catch and handle appropriately.

Validation Options

Mongoose-unique-validator provides a few options that you can use to customize its behavior.

message

The message option lets you customize the message that is shown when the validation fails. By default, Mongoose-unique-validator uses the following message:

Path `{PATH}` must be unique.

You can customize this message by passing a string or a function to the message option. If you pass a function, it will receive the value of the field as its argument, like so:

const userSchema = new mongoose.Schema({
  email: {
    type: String,
    required: true,
    unique: true,
    uniqueCaseInsensitive: true,
    validate: {
      validator: function (value) {
        return /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(value);
      },
      message: props => `${props.value} is not a valid email address!`
    },
  }
});

userSchema.plugin(uniqueValidator, { message: 'Error, expected {PATH} to be unique.' });

In this example, we pass a custom message string to the uniqueValidator() function.

uniqueCaseInsensitive

By default, Mongoose-unique-validator performs case-sensitive validation. However, you can make the validation case-insensitive by setting the uniqueCaseInsensitive option to true.

const userSchema = new mongoose.Schema({
  email: {
    type: String,
    required: true,
    unique: true,
    uniqueCaseInsensitive: true,
  },
});

userSchema.plugin(uniqueValidator);

In this example, we set the uniqueCaseInsensitive option to true for the email field.

Conclusion

Mongoose-unique-validator is a useful plugin for validating unique fields in MongoDB documents. It simplifies the validation process and provides customizable options for message and case-insensitivity. By including Mongoose-unique-validator in your project, you can ensure that your MongoDB documents have unique fields, making your application more secure and reliable.