validate-function-params
JavaScript library that assists with function parameter validation at runtime, through an easy-to-use Fluent API.
Installation
$ npm install validate-function-params
Usage
The following examples demonstrate how to use this library.
Basic Usage
The simplest way to validate a function:
const validate validations = ; const add = a + b; const validAdd = add; // This call will produce the following error// Error: Function add(number, number)// Error: 1st parameter must be of type number// Error: 2nd parameter must be of type number; // This call will produce the following error// Error: Function add(number, number)// Error: 2nd parameter must be of type number; // This call will successfully return 3;
Object Method Validation
Similarly you can validate object methods the following way:
const validate validations = ; { const validBark = thisbark; thisbark = validBark; } { for let i = 0; i < times; i++ console; } const dog = ; // This call will produce the following error// Error: Function bark(string, number)// Error: 1st parameter must be of type string// Error: 2nd parameter must be of type numberdog;
You can optionally bind validate
to a this
context to have its constructor name appear
in the error log:
const validate validations = ; { const boundValidate = validate; const validBarkWithThis = thisbark; thisbark = validBarkWithThis; } { for let i = 0; i < times; i++ console; } const dog = ; // This call will produce the following error// Error: Function Dog.bark(string, number) <-- Notice 'Dog.' appearing before the method name// Error: 1st parameter must be of type string// Error: 2nd parameter must be of type numberdog;
Complex Validations
You can validate any number of specific parameters without listing the ones you do not care about:
const validate validations = ; const complex = { /*...*/}; const validComplex = complex; // This call will produce the following error// Error: Function complex(undefined, any, any, any, object, any, function, any, boolean)// Error: 1st parameter must be of type undefined// Error: 5th parameter must be of type object// Error: 7th parameter must be of type function// Error: 9th parameter must be of type boolean;
API
export | type | params | description |
---|---|---|---|
validate | function | ...validations: Function[] | any number of validation functions described below |
validations | object | namespace object containing validations described below |
validations
namespace | type | params | description |
---|---|---|---|
firstParam | object | namespace object containing validations described below | |
secondParam | object | namespace object containing validations described below | |
thirdParam | object | namespace object containing validations described below | |
nthParam | function | n: number | function that accepts a number and returns corresponding validations described below |
firstParam.mustBe
| secondParam.mustBe
| thirdParam.mustBe
| nthParam(n).mustBe
The following validation functions are provided under the above namespaces, with similar behaviour differing only in the parameter index (1-based) and the expected type.
validation name | description |
---|---|
string | expects a string |
number | expects a number |
boolean | expects a boolean |
function | expects a function |
object | expects an object |
undefined | expects undefined |
Notes
The intermediate mustBe
namespace exists only to add to the fluency of the API, containing the actual validation functions.
Examples
To validate the:
1st param as string you would need:
validations.firstParam.mustBe.string
2nd param as number you would need:
validations.secondParam.mustBe.number
3rd param as boolean you would need:
validations.thirdParam.mustBe.boolean
4th param as object you would need:
validations.nthParam(4).mustBe.object
5th param as function you would need:
validations.nthParam(5).mustBe.function
6th param as undefined you would need:
validations.nthParam(6).mustBe.undefined
To use the validations you would need something like:
const func = {}; const validFunc = func;