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

sails-hook-validator

v1.0.0

Published

Validations in Sailsjs requests

Downloads

103

Readme

sails-hook-req-validator

Build Status

Sails hook for validate request.

  npm install --save sails-hook-validator

###req.validator();

######Requirements: Sails v1.0.0 and lodash enabled as global (by default it comes enabled) and node 6 >=

If something goes wrong it return a 400 to the user with the error, if it goes ok it returns the params. It works as a filter too, for example if the client sends name and surname but we only want to work with the name:

  // req.params.all() === {name: 'joseba', surname: 'legarreta'}

  var param = req.validator('name');

  // param === {name: 'joseba'}

  // MORE EXAMPLES
  // For more that one params the required params have to go in an Array
  // req.params.all() === {id: 1, name: 'joseba'}

  var params = req.validator(['id', 'password']);

  // params === false && the client has a 400 - password is required
  // so we end the controller execution

  if(!params) return null;
  // If we have params continue the logic
  User.update(params.id, params).exec(function(){}); //...

  // MORE STUFF

  // Not sending the default 400 code with error text
  // Just set the second params as false.
  var params = req.validator(['nickname', 'email', 'password', '?name'], false);

  // In case of error params === false else the params will be an object with values

  if(params) return res.ok(); else return res.badRequest('Custom message');

  // ASYNC WAY GET ERROR AND PARAMS

  var filter = [
    'id', '?name',
    {'?surname': ['string', 'toUpper'], height: 'float', '?age': 'int'}
  ];
  req.validator(filter, false, function(err, params){
    // err === {message: 'parsedError...', invalidParameters: ['invalid', 'parameter', 'list']}
    if(err) return res.badRequest(err.message);
    return res.ok(params);
  });

  // OR

  var filter = [
    'id', '?name',
    {'?surname': ['string', 'toUpper'], height: 'float', '?age': 'int'}
  ];
  req.validator(filter, function(err, params){ // If error the validator will send the req.400
    if(params) return res.ok(params);
  });

If we want to check the type we can ask for it, for example: int, email, boolean, float... req.validator checks if it is the type that we are looking for or if it's posible to convert to the type that we want (ex: 'upper' check if is upperCase text, 'toUpper' upperCase the text if the value is a string, if it couldn't upperCase it the client will get an 400).

If it can't convert or the types doesn't match, it will send the 400 error to the client. Example:


  // req.params.all() === {id: 1, likes: '12.20', url: 'HttP://GOOGLE.eS', email: '[email protected]'}
  var params = req.validator(['id', {likes: 'int', url: ['url', 'toLower'], email: 'email'}]);
  // params = {id: 1, likes: 12, url: 'http://google.es', email: '[email protected]'}

  // MORE EXAMPLES

  // req.params.all() === {id: 1, likes: '12.20', url: 'http://google.es', email: '[email protected]'}
  var params = req.validator(['id', 'url', {likes: 'float', email: 'email'}]);
  // params = {id: 1, likes: 12.20, url: 'http://google.es', email: '[email protected]'}

  // MORE

  // req.params.all() === {id: 1, likes: 'hello', url: 'http://google.es', email: '[email protected]'}
  var params = req.validator(['id', {url: ['url', 'lower'], likes: 'float', email: 'email'}]);
  // params === false and the client gets a res 400 - 'likes' has to be a float

  // More examples

  var param = req.validator({color: ['hexcolor', 'upper']});

  // More examples

  // Optional values

  var param = req.validator('?nickname', {color: ['hexcolor', 'upper'], '?name': 'toUpper'});

  // If we have a nickname and/or a name parameters it will return it to the param var applying the rules
  // If nickname or/and name are undefined in the request, it will ignore them and won't send 400
Validation types (for now, maybe I will add more)
  'email'
  'toEmail'
  'url'
  'ip'
  'alpha'
  'numeric'
  'base64'
  'hex'
  'hexColor'
  'lower'
  'toLower'
  'upper'
  'toUpper'
  'string'
  'boolean'
  'toBoolean'
  'int'
  'float'
  'date'
  'toDate'
  'json'
  'ascii'
  'mongoId'
  'alphanumeric',
  'creditCard'

Tests

To test this hook, you need mocha installed in your computer globally.

npm install -g mocha // Just if you don't have mocha installed yet

// And then just run mocha in the hook folder

mocha

// Optional: Change port or log level

log=info port=1234 mocha

// log level options = error, warn, info, verbose and silly. By default: warn
// port by default: 1992