options-checker
v1.1.0
Published
check mandatories options, even with conditionals
Downloads
2
Readme
options-checker
11/10/18
the heck!?111
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!
example
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"]
// }
use with functions
const field = rndm(19)
const result = Module({
field,
action: 'test'
}, {
action: true,
field (value, key) {
return value === field && key === 'field'
}
})