npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-readwrite

v2.2.0

Published

Remove unauthorized properties from inputs and outputs

Downloads

4

Readme

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 = require('mongoose-readwrite').default

ES2015+

import readwrite from 'mongoose-readwrite'

Basic setup

// Define your Schema
const UserSchema = new mongoose.Schema({
  name: String,
  email: String,
  password: String,
})

// Add mongoose-readwrite plugin and define rules on the SchemaTypes
UserSchema.plugin(readwrite, {
  rules: {
    email: {
      writable: false,
    },
    password: {
      readable: false,
      writable: false,
    },
  },
})

// ...
// Compile your model
const User = mongoose.model('User', UserSchema)

Basic input filtering

// Create a filter
const filterInput = User.getInputFilter()

// Pass an object (for example the request body) to the filter
const filteredBody = filterInput({
  name: 'Harry',
  email: '[email protected]',
  password: 'fakepassword',
})

// The result removes any unauthorized property from the input
console.log(filteredBody) // { name: 'Harry' }

Basic output filtering

// Get a mongoose document, you can also get if from a query
const Harry = new User({
  name: 'Rowan',
  email: '[email protected]',
  password: 'realpassword',
})


// Call redact on the object and obtain a safe output with all non-readable properties removed
console.log(Harry.redact()) // { name: 'Rowan', email: '[email protected]' }

For more usage cases, check the examples and tests

API Reference

Setup

UserSchema.plugin(readwrite, {
  /*
  The rules object is parallel to the Schema definition object.
  For each SchemaType defined on the Schema, the rules object can contain
  an object with rules for the path. The allowed rules are readable and writable
  All rules accept a Boolean or [String] value.
  The boolean value applies to all filter and redact operations.
  An empty array equals to false.
  If an array of strings is passed, each string value will be treated as a persona
  for filter and redact operations.
  */
  rules: {
    /*
    Rules accept Boolean or [String].
    */
    readable: Boolean || [String]
    writable: Boolean || [String]
  },
})

getInputFilter

/**
  * Returns an input filter function
  * @param {object} [options] - Options object for filter creation
  * @param {object} [options.subdocument] - The dot path of a subdocument on the parent schema e.g. 'socialInfo.friends'
  */
const filter = User.getInputFilter(options)

apply filter

/**
  * Returns filtered data
  * @param {object} data - Data object to be filtered
  * @param {object} [persona] - represents a role that has certain write permissions
according to the rules defined on setup. e.g. 'Admin'
If persona is undefined, a field is writable or readable if the rule is not
present (the field is not restricted) or if the rule has a value of true.
  */
const filteredData = filter(data, persona)

License

(The MIT License)

Copyright (c) 2017 Mario Ferreira <[email protected]>

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