Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/niradler/params-check
JS function param check, use reusable adapters to check function params, for native js projects.
https://github.com/niradler/params-check
function params params-check params-validate
Last synced: 20 days ago
JSON representation
JS function param check, use reusable adapters to check function params, for native js projects.
- Host: GitHub
- URL: https://github.com/niradler/params-check
- Owner: niradler
- Created: 2018-10-29T22:17:55.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-07T14:26:45.000Z (almost 5 years ago)
- Last Synced: 2024-11-21T01:15:08.140Z (about 1 month ago)
- Topics: function, params, params-check, params-validate
- Language: JavaScript
- Homepage:
- Size: 34.2 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### Params Check
```javascript
//npm i -S params-check --production
const ParamsCheck = require('params-check')
//optional container to catch the error throw;
const funcContainer = ParamsCheck.funcContainer;
// test adapter to check number of params send to function
const adapter = (params, schema) => {
if(params.length == schema.numberOfParams) {
return {error:null}
}
return {error:'missing params!'}
};
// create instance with adapter for validation
const paramsCheck = new ParamsCheck(adapter);
// test function
const testFunc = (a, b, c) => a + b + c;
const validationRules = {
numberOfParams:3
}
// composition
const testedFunc = paramsCheck.withValidation(testFunc.name, validationRules, testFunc);test('to throw', () => {
let isThrow = false;
//will throw error missing param, got 2 instead of 3;
funcContainer(()=>testedFunc(1, 2),(error)=> {console.log(error); isThrow = true;});
expect(isThrow).toBe(true);
});test('to pass', () => {
expect(testedFunc(1, 2, 3)).toBe(6);
});
```## Demo
[runkit](https://runkit.com/niradler/5bd797402b601900123e01c4)