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-actor

v0.0.5

Published

A mongoose plugin to validate each path of a model using the value of the path and the actor who is triggering the action to the model. You can add validation according to the triggered method. Ideal to validate roles permission on path and method level.

Downloads

2

Readme

mongoose-actor

A mongoose plugin to validate each path of a model using the value of the path and the actor who is triggering the action to the model. You can add validation according to the triggered method. Ideal to validate roles permission on path and method level.

use case

Imagine you have an Person Model. You want to let all people change it own weigth, for example. But you don't want to let people change their birthdate, because it's an official data. So, only authorized people (like federal agents) should be able to change that information. That's when mongoose-actor join the party.

who is the actor?

We are using the word actor to represent the author of the action (update, save, find...). Using the example above, if the actor is not an federal agent, they should not be able to update that information.

how does it work

When you plug mongoose-actor in your schema, all of your paths which have the property validateAccess will be able to validate the actor when some mongoose method is triggered, having the ability to make custom validations per field per method.

The validation will receive the value inserted and the actor. It must return a boolean. If validation returns true, it will pass. Else, it will throw an error with the errorMessage.

In the example below, only federal agents can save, findOneAndUpdate or remove the property name of any User.

import mongoose from 'mongoose'
import mongooseActor from 'mongoose-actor'

const _onlyFederalAgentAllowed = (val, actor) => actor.isFederalAgent 

const userSchema = mongoose.Schema({
  name: { 
    type: String, 
    required: true,
    validateAccess: {
      save: _onlyFederalAgentAllowed,
      findOneAndUpdate: _onlyFederalAgentAllowed,
      remove: _onlyFederalAgentAllowed
    }
  },
  email: { 
    type: String, 
    required: true, 
    unique: true
  }
})

userSchema.plugin(mongooseActor)

export default mongoose.model('User', userSchema)

Later, when you call the mongoose methods, you must call together the actor method passing the actor:

// Updating an user
async User
  .findOneAndUpdate({ name: 'João' }, { name: 'John' })
  .actor({ isFederalAgent: true })

// Saving a new user
async User({ name: 'João' })
  .actor({ isFederalAgent: true })
  .save()

errorMessage

You can pass to your plugin an options object configuration. For now you only can set the errorMessage:

const config = {
  // Write your custom message.
  // If you don't, it will fallback to: `Access denied to ${path}`.
  errorMessage: ({ path }) => `U can't touch this: ${path}.`
}

userSchema.plugin(mongooseActor, config)