options-checker
Version:
check mandatories options, even with conditionals
85 lines (68 loc) • 1.94 kB
Markdown
> 11/10/18
[](https://standardjs.com)
[](https://travis-ci.org/brugnara/options-checker)
[](https://npm.runkit.com/options-checker)
Use this to check if all params are valid. All of them can be
overridden with the `_id` section. The parameter will change
its requirements while assuming the value passed.
In this example, when `action` assumes the value `addE`,
`name` became not mandatory but `fromName` and `toName` yes.
The example also shows that you can pass values you accept.
`true` simply means the key is mandatory to exists.
**NEW!** callback function support!
```js
const neededOptions = {
action: ['addE', 'addV', 'drop'],
label: true,
name: true,
_if: {
action: {
'addE': {
name: false,
fromName: true,
toName: true
}
}
}
}
```
> node example.js --action addV --name test --label lbl
```
const result = optionsChecker(require('minimist')(process.argv), neededOptions)
// {
// "valid": true
// }
```
> node example.js --action addE --name test --label lbl
```
const result = optionsChecker(require('minimist')(process.argv), neededOptions)
// {
// "valid": false,
// "option": "nameFrom"
// }
```
> node example.js --action addM --name test --label lbl
```
const result = optionsChecker(require('minimist')(process.argv), neededOptions)
// {
// "valid": false,
// "option": "action",
// "validOptions": ["addE", "addV", "drop"]
// }
```
```js
const field = rndm(19)
const result = Module({
field,
action: 'test'
}, {
action: true,
field (value, key) {
return value === field && key === 'field'
}
})
```