@marcelbraun/enapso-args
v1.0.1
Published
Enapso Smart Arguments Processor
Downloads
7
Maintainers
Readme
Enapso Smart Arguments Processor
You can either pass a single object as arguments or the arguments in the defined order.
Why to use the Enapso Smart Arguments Processor?
You avoid unexpected results due to wrong or missing arguments or due to wrong argument order. You can specify defaults for missing arguments and valdiate your arguments against a given schema.
Installation
npm i @marcelbraun/enapso-args --save
Usage
Initialization
const
{ EnapsoArguments } = require('@marcelbraun/enapso-args')
;
global.enargs = new EnapsoArguments();
global.enGetArgs = enargs.getArgs;
Arg Validation Schema
const
schemaSingleStringArg = {
args: [
{
"identifier": "text",
"type": "string",
"required": false,
"default": "default value",
"minLength": 0,
"maxLength": 20
}
]
};
Argument Validation
function demoSingleStringArg(/*text*/) {
let args = enGetArgs(arguments, schemaSingleStringArg, logValidationViolation);
console.log('demoSingleStringArg: ' + args.text);
}
function demo() {
demoSingleStringArg("Test");
demoSingleStringArg(1);
demoSingleStringArg(true);
demoSingleStringArg();
}
Output
demoSingleStringArg: Test
Error: Argument 'text' must be of type 'string' but is of type 'number'. Value: 1
demoSingleStringArg: 1
Error: Argument 'text' must be of type 'string' but is of type 'boolean'. Value: true
demoSingleStringArg: true
demoSingleStringArg: default value