A package of mongoose types extensions.
npm install --save mongoose-types-ext
gulp test
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);
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).
})
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.
});
I am very glad to see this project living with pull requests.
Copyright (c) 2015 Daniel Campos
Licensed under the MIT license.