Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kjirou/parry-mongoose
mongoose binding for parray
https://github.com/kjirou/parry-mongoose
Last synced: 12 days ago
JSON representation
mongoose binding for parray
- Host: GitHub
- URL: https://github.com/kjirou/parry-mongoose
- Owner: kjirou
- License: mit
- Created: 2015-02-27T04:16:13.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2021-08-31T07:27:06.000Z (about 3 years ago)
- Last Synced: 2024-09-30T06:39:53.859Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# parry-mongoose
[![npm version](https://badge.fury.io/js/parry-mongoose.svg)](http://badge.fury.io/js/parry-mongoose)
[![Build Status](https://travis-ci.org/kjirou/parry-mongoose.svg?branch=master)](https://travis-ci.org/kjirou/parry-mongoose)[mongoose](https://github.com/learnboost/mongoose) binding for [parry](https://github.com/kjirou/parry).
## 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` evaluates `schemaType.validate` in reverse order
- In the previous example, `isLength` has been evaluated first
- Can not validate a field if value is empty and `schemaType.required` is `false`
- It is also a specification of mongoose
- Ref
- [mongoose schemaType.validate](http://mongoosejs.com/docs/api.html#schematype_SchemaType-validate)