Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ajpocus/mongoose-filter-properties
Filter your model properties with readable/writeable flags.
https://github.com/ajpocus/mongoose-filter-properties
Last synced: 14 days ago
JSON representation
Filter your model properties with readable/writeable flags.
- Host: GitHub
- URL: https://github.com/ajpocus/mongoose-filter-properties
- Owner: ajpocus
- Created: 2013-08-02T21:32:03.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-08-03T00:30:31.000Z (over 11 years ago)
- Last Synced: 2024-04-25T20:03:53.105Z (9 months ago)
- Language: JavaScript
- Size: 133 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# mongoose-filter-properties
filter properties by readable/writeable flags, protecting your models## Intro
You can install mongoose-filter-properties via npm:
npm install mongoose-filter-properties
This module allows you to explicitly filter readable/writeable attributes on your models. This is useful if you want to quickly filter user input, or expose a limited subset of properties to the user. You can find examples below, in the "Usage" section.## Usage
In your model file, require the module like so:
var filterProperties = require('mongoose-filter-properties');
In the model definition, you can define readable/writeable properties like this:var userSchema = new mongoose.Schema({
name: { type: String, writeable: false },
email: { type: String, readable: false }
}, { safe: true });
userSchema.plugin(filterProperties);
var User = mongoose.model('User', userSchema);Please note, that "readable" and "writeable" are simply filters. You can always assign a value to these fields. These properties only come into effect if you call the "filter" method, like so:
var user = new User({ name: "Foo Bar", email: "[email protected]" });
user.filter('readable', function (err, user) {
console.log(user); // "email" will not be visible
});
var userParams = req.body.user;
User.filter(userParams, "writeable", function (err, userParams) {
// userParams is safe to assign, even if the user injected some protected value
});## Testing
Since this is a relatively small and simple module, I wrote a couple of minimal tests in `test.js`. You can run the tests like so:
mocha ./test.js