Skip to content Skip to sidebar Skip to footer

How Do I Extend Sails.js Library

I would like to extend sailsjs to have something similar to rails strong params i.e. req.params.limit(['email', 'password', 'password_confirmation']) I have created this already a

Solution 1:

i think you could just create a policy

// policies/limit.js
limit = function(limiters){
params = this.all();
self = Object();
limiters.forEach(function(limiter){
  if(params[limiter]){
    self[limiter] = params[limiter]
  }
returnself;
}

module.exports = functionlimit (req, res, next) {
  req.params.limit = limit;
  req.params.limit(["email", "password"]);
  next();
};

then you can add the policy in your ./config/policies.js file. the example is for all controllers/actions. in the link above is the documentation on how to add it to specific actions.

// config/policies.jsmodule.exports.policies = { 
'*': 'limit'
};

EDIT: of course you can do the call to req.params.limit(...); in your controller if you don't want it static in your policy. policies are in general nothing more than express middlewares

Solution 2:

I see the use cases for this on controller actions, limiting possible searches and so on.

On the other hand: When you just want to your model not to take up undefined attributes found in a create request you could simply set schema to true for the corresponding model:

// models/example.jsmodule.exports = {
  schema: true,
  attributes: {...}
}

Setting it globally:

// config/models.jsmodule.exports.models = {
  schema: true,
  ...
}

Post a Comment for "How Do I Extend Sails.js Library"