an argument validator [originally] for
yargs
import argsert from 'argsert';
let passed;
try {
passed = argsert([configurationString], arguments);
} catch (err) {
if (err instanceof TypeError) {
// a type was missing or incorrectly given
} else {
// otherwise, there was something wrong with the configuration
// or the number of args passed in.
}
}
import argsertPromise from 'argsert/promise';
argsertPromise([configurationString], arguments)
.then(passed => passed)
.catch(err => {
// same error as above
});
array
boolean
buffer
error
function
null
number
object
promise
[that passes then/is-promise]string
symbol
undefined
allows for any type.
space-separated entries with the following syntax:
'[string|number] [object]'
:
- the first argument can be a string OR a number, or undefined
- the second argument can be an object literal, or undefined.
'<object> <*>'
:
- the first argument must be an object literal
- the second argument can be any type, but must be provided.
The following leverages argsert
best because it is the most performant and easiest to read by:
function nodeStyleCallback(err, result) {
// ...
argsert.apply('<error|undefined|null> [object|string]', arguments);
// ...
}
You can also look at the example tests for other ways to invoke argsert
.
To learn more you can read about JS' function methods: apply
, bind
, and call
.