validate-params

1.0.0 • Public • Published

validate-params

A small parameter validation library

The purpose of this library is to easily check that the contract of your functions are not violated; and if they are, to allow you to gracefully handle the situation (provide useful error messages).

Build Status Coveralls Status Code Climate

Basic examples

var Validator = require('validate-params');
 
// validating a single argument
function simpleWrapper(callback){
    Validator.assert.arg(callback, 'function');
    //Then do something with the callback later
    //At this point we can be sure that callback is a function
}
 
simpleWrapper(42); //throws Error('Expected type "function" but it was type "number"')
 
// validating an argument object
function myCoolFunction(opts){
    Validator.assert.arg(opts,
    {name: 'string',
    //handles optional parameters
     phoneNum: {
        type: 'string',
        optional: true
     },
     //handles nested objects
     location: {
        city: 'string',
        longitude: 'number',
        latitude: 'number'
     }
    });
    //...
}
 
myCoolFunction({name: 'Jenny',
                phoneNum: '876-5309',
                location: {
                   city: 'Chicago',
                   longitude: 87.6847,
                   latitude: 41.8369
                }
               });
 

Documentation

To learn more checkout the documentation page

License

MIT - view the full license here

Package Sidebar

Install

npm i validate-params

Weekly Downloads

250

Version

1.0.0

License

MIT

Last publish

Collaborators

  • mdvorscak