mongoose-readwrite
Remove unauthorized properties from inputs and outputs. Easily protect your input and redact your outputs without fuss.
Installation
npm install --save mongoose-readwrite
Usage
Import
ES5
var readwrite = default
ES2015+
Basic setup
// Define your Schemaconst UserSchema = name: String email: String password: String // Add mongoose-readwrite plugin and define rules on the SchemaTypesUserSchema // ...// Compile your modelconst User = mongoose
Basic input filtering
// Create a filterconst filterInput = User // Pass an object (for example the request body) to the filterconst filteredBody = // The result removes any unauthorized property from the inputconsole // { name: 'Harry' }
Basic output filtering
// Get a mongoose document, you can also get if from a queryconst Harry = name: 'Rowan' email: 'real@email.com' password: 'realpassword' // Call redact on the object and obtain a safe output with all non-readable properties removedconsole // { name: 'Rowan', email: 'real@email.com' }
For more usage cases, check the examples and tests
API Reference
Setup
UserSchema
getInputFilter
/** * Returns an input filter function * @param * @param */const filter = User
apply filter
/** * Returns filtered data * @param * @paramaccording to the rules defined on setup. e.g. 'Admin'If persona is undefined, a field is writable or readable if the rule is notpresent (the field is not restricted) or if the rule has a value of true. */const filteredData =
License
(The MIT License)
Copyright (c) 2017 Mario Ferreira <ztrange@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Changelog
1.0.0
- Initial commit
2.0.0-2.0.4
- Refactor and tests
- Documentation on README
- Examples