Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Validators for mongoose models utilising node-validator
JavaScript
branch: master

This branch is 2 commits ahead, 37 commits behind leepowellcouk:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib Fixed notEmpty
.gitignore
README.md
index.js
package.json

README.md

Mongoose Validator

Validators for Mongoose schemas utilising node-validator.

Mongoose Validator simply returns Mongoose style validation objects that utilise node-validator for the data validation.

Installation

npm install mongoose-validator

Usage

var mongoose = require('mongoose'),
    mongooseValidator = require('mongoose-validator'),
    validator = mongooseValidator.validator;

var nameValidator = [validator.len(3, 50), validator.isAlphanumeric()];

var Schema = new mongoose.Schema({
    name: {type: String, required: true, validate: nameValidator}
});

Error objects are returned as normal via Mongoose.

Error Messages

By default error messages are used from node-validator, however you can use your own by passing them into the validation method.

validator.len(3, 50, 'Name should be between 3 and 50 characters in length')

Alternatively you can overwrite any of the defaultError messages from node-validator for use in all calls to a particaular method.

var mongoose = require('mongoose'),
    mongooseValidator = require('mongoose-validator'),
    validator = mongooseValidator.validator;

// batch

mongooseValidator.setDefaultErrorMessage({
    len: 'A new error message that will be used for all len calls',
    isRegex: 'A new error message that will be used for all isRegex calls'
});

// single

mongooseValidator.setDefaultErrorMessage('len', 'A new error message that will be used for all len calls');
Something went wrong with that request. Please try again.