Coder Social home page Coder Social logo

mongoose-validator's Introduction

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'),
	validate = require('mongoose-validator').validate;

// validate([method], [arg1], [arg2] ... );

var nameValidator = [validate('len', 3, 50), validate('isAlphanumeric')];

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

Error objects are returned as normal via Mongoose.

##Error Messages##

Custom error messages are now back in 0.2.1 and can be set through the options object:

validate({message: "String should be between 3 and 50 characters"}, 'len', 3, 50)

Pass validation if empty

Some of node-validator validators require a value to check against (isEmail, isUrl etc). There may be instances where you don't have a value to check i.e. a path that is not required and as such these few validators return an false value causing validation to fail. This can now be bypassed by setting the passIfEmpty option:

var validator = validate({passIfEmpty: true}, 'isUrl');

website: {type: String, validate: validator};

Custom validators

As of 0.2.1 custom validators can be added:

require('mongoose-validator').extend('isBoolean', function () {
    return 'boolean' === typeof this.str;
}, 'Not a boolean');

require('mongoose-validator').extend([method name], [validator], [default error message]);

Custom validators are called normally:

validate({passIfEmpty: true}, 'isBoolean');

Arguments can be passed like so:

require('mongoose-validator').extend('isType', function (type) {
    return type === typeof this.str;
}, 'Not correct type');

validate('isType', 'string');

NOTE: As per node-validator documentation, the currently tested value is accessed through this.str.

##Contributors##

Special thanks to Francesco Pasqua for heavily refactoring the into something far more future proof.

mongoose-validator's People

Contributors

cesconix avatar tjmehta avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.