parry-mongoose
v1.0.0
Published
mongoose binding for parry
Downloads
2
Readme
parry-mongoose
Installation
npm install parry-mongoose
Usage
var mongoose = require('mongoose');
var parry = require('parry');
var parryMongoose = require('parry-mongoose');
var UsernameField = parry.Field.extend()
.type('isAlpha')
.type('isLength', [4, 8])
;
var userSchema = new mongoose.Schema({
username: {
type: String,
required: true,
validate: parryMongoose(UsernameField)
}
});
var User = mongoose.model('User', userSchema);
var user = new User();
user.username = 'ab1';
user.validate(function(err) {
console.log(err);
// ->
//
// { [ValidationError: Validation failed]
// message: 'Validation failed',
// name: 'ValidationError',
// errors:
// { username:
// { [ValidatorError: String is not in range]
// message: 'String is not in range', // isLength error
// name: 'ValidatorError',
// path: 'username',
// type: 'user defined',
// value: 'ab1' } } }
//
});
Note:
- mongoose's
doc.validate
evaluatesschemaType.validate
in reverse order- In the previous example,
isLength
has been evaluated first
- In the previous example,
- Can not validate a field if value is empty and
schemaType.required
isfalse
- It is also a specification of mongoose
- Ref