0.0.1 • Published 10 years ago

valid-params v0.0.1

Weekly downloads
2
License
-
Repository
github
Last release
10 years ago

valid-params

This library is used to extract values from an objet and return them with some transformations. It can be used to valid the parameters received from a web application.

Usage

var validParams = require('valid-params');

var params = {
  firstName:   "Antoine",
  lastName:    "Proulx",
  phoneNumber: "123 456-7890",
  admin:       true
};

// The model defines what will be the object returned by validParams. The value
// of the key is a function that takes an argument representing the parameters
// to valid. This function should extract the required parameter(s) from it,
// transform it/them (if needed) and return the value. If it only needs to take
// the literal value from the params object, just assign the name of the the
// key.
var model  = {
  firstName:   'firstName',
  lastName:    'lastName',
  phoneNumber: function(params) {
    return params.phoneNumber.replace(/\D/g, "");
  }
};

validParams(params, model);
// Result
// { firstName: "Antoine", lastName: "Proulx", phoneNumber: "1234567890" }