Validate glob options
npm install validate-glob-opts


Validate node-glob options
``javascriptsync
validateGlobOpts({
sync: true,
mark: '/',
caches: {}
});
/* => [
Error: option is deprecated and there’s no need to pass any values to that option, but true was provided.,mark
TypeError: node-glob expected option to be a Boolean value, but got '/' (string).,caches
Error: node-glob doesn't have option. Probably you meant cache.`
] */
``
npm install validate-glob-opts
`javascript`
const validateGlobOpts = require('validate-glob-opts');
obj: Object (glob options) Array
customValidations: Array
Return:
It strictly validates glob options, for example,
* It disallows the deprecated sync option to receive any values.cwd
* It disallows String options e.g. to receive non-string values.stat
* It disallows Boolean options e.g. to receive non-boolean values.symlinks
* It disallows Object options e.g. to receive non-object values.symlink
* It invalidates probably typoed option names e.g. .
Then, it returns the validation result as an array of error objects.
`javascript
const validateGlobOpts = require('validate-glob-opts');
const ok = {
root: '/foo/bar/',
nodir: true,
ignore: ['path1', 'path2'],
symlinks: {}
};
validateGlobOpts(ok); //=> []
const notOk = {
root: Buffer.from('Hi'),
nodir: NaN,
ignore: ['path1', 1],
symlink: {}
};
const results = validateGlobOpts(notOk);
results.length; //=> 4
results[0];
//=> TypeError: node-glob expected root option to be a directory path (string), but got nodir
results[1];
//=> TypeError: node-glob expected option to be a Boolean value, but got NaN (number).ignore
results[2];
//=> TypeError: Expected every value in the option to be a string, but the array includes a non-string value 1 (number).symlink
results[3];
//=> Error: node-glob doesn't have option. Probably you meant symlinks.`
#### User-defined validation
You can provide your own validations by passing an array of functions to the second parameter. Each function receives the object and should return an error when the object is not valid.
`javascriptrealPath
validateGlobOpts({realPath: true, nodir: true});
/*=> [
Error: node-glob doesn't have option. Probably you meant realpath.
] */
validateGlobOpts({realPath: true, nodir: true}, [
obj => obj.nodir ? new Error('My app doesn\'t support nodir option.') : nullrealPath
]);
/*=> [
Error: node-glob doesn't have option. Probably you meant realpath.nodir
TypeError: My app doesn't support option.``
] */
ISC License © 2018 Shinnosuke Watanabe