Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/weisjohn/mongoose-reference-validator
- Owner: weisjohn
- License: mit
- Created: 2015-07-23T04:02:22.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-07-23T05:28:06.000Z (over 9 years ago)
- Last Synced: 2024-09-19T03:37:06.754Z (3 months ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/mongoose-reference-validator
- Size: 133 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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).