assisrafael / mongoose-types-ext

A package of mongoose types extensions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

npm version Build Status Coverage Status

mongoose-types-ext

A package of mongoose types extensions.

Instalation

With npm

    npm install --save mongoose-types-ext

Runing tests

	gulp test

Usage

Just require the extensions before load your mongoose models:

var mongoose = require('mongoose');
require('mongoose-types-ext')(mongoose);
var YourSchemaDefinition = new mongooseSchema({
	someField: {
		type: String,
		maxLength: 10
	},
	/* (...) */
});
var YourModel = mongoose.model('YourModel', YourSchemaDefinition);

Supported extentions

String

  • minlength: Sets a min length string validator.
  • maxlength: Sets a max length string validator.
  • exactLength: Sets a exact length string validator.

Custom error messages: You can also configure custom error messages and use the special tokens: {MIN_LENGTH}, {MAX_LENGTH} and {EXACT_LENGTH} which will be replaced with the invalid value. Ex:

var max = [4, 'The length of path `{PATH}` ({VALUE}) is beneath the limit ({MAX_LENGTH}).'];
var schema = new Schema({ n: { type: String, maxlength: max })
var M = mongoose.model('Measurement', schema);
var s= new M({ n: 'teste' });
s.validate(function (err) {
	console.log(String(err)); // ValidationError: The length of path `n` (5) is beneath the limit (4).
})

Number

  • exclusivemin: Sets a minimum number validator not including the configurated value.

Custom error messages: You can also configure custom error messages and use the special token {EXCLUSIVE_MIN} which will be replaced with the invalid value. Ex:

var min = [10, 'The value of path `{PATH}` ({VALUE}) should be greater than ({EXCLUSIVE_MIN}).'];
var schema = new Schema({ n: { type: Number, min: min })
var M = mongoose.model('Measurement', schema);
var s= new M({ n: 4 });
s.validate(function (err) {
	console.log(String(err)); // ValidationError: The value of path `n` (4) should be greater than 10.
});

How to contribute

I am very glad to see this project living with pull requests.

LICENSE

Copyright (c) 2015 Daniel Campos

Licensed under the MIT license.

About

A package of mongoose types extensions

License:MIT License


Languages

Language:JavaScript 100.0%