Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/weisjohn/mongoose-reference-validator

validator for external reference documents
https://github.com/weisjohn/mongoose-reference-validator

Last synced: about 2 months ago
JSON representation

validator for external reference documents

Awesome Lists containing this project

README

        

# mongoose-reference-validator

validator for external reference documents

### usage

schema setup

```javascript
var mongoose = require('mongoose');
var ObjectId = mongoose.Schema.Types.ObjectId;
var mrf = require('mongoose-reference-validator');

var user = new mongoose.Schema({ name: String });
mongoose.model('user', user);

var car = new mongoose.Schema({
name: String,
driver : { type: ObjectId, ref: 'user' },
});
mrf(car);
mongoose.model('car', car);
```

document interaction

```javascript
var jetta = new car({ name : "Jetta", user : user._id });
jetta.save(function(err, doc) {
if (err && err.errors.driver.message) {
console.log('user._id was invalid');
console.log(err.errors.driver.message); // driver does not exist
}
});
```

The [validator](http://mongoosejs.com/docs/validation.html#update-validators) will run when being saved, however, you can also run while [updating](http://mongoosejs.com/docs/validation.html#update-validators).