🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

validate-glob-opts

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

validate-glob-opts - npm Package Compare versions

Comparing version

to
0.4.0

48

index.js

@@ -9,3 +9,5 @@ /*!

const arrayToSentence = require('array-to-sentence');
const isPlainObj = require('is-plain-obj');
const indexedFilter = require('indexed-filter');

@@ -61,3 +63,25 @@ const ERROR_MESSAGE = 'Expected node-glob options to be an object';

module.exports = function validateGlobOpts(obj) {
const isNonFn = val => typeof val !== 'function';
const stringifyFilterResult = obj => `${inspect(obj.value)} (at ${obj.index})`;
module.exports = function validateGlobOpts(obj, additionalValidations) {
if (additionalValidations !== undefined) {
if (!Array.isArray(additionalValidations)) {
throw new TypeError(`Expected an array of functions, but got a non-array value ${
inspect(additionalValidations)
}.`);
}
const nonFunctions = indexedFilter(additionalValidations, isNonFn);
const nonFunctionCount = nonFunctions.length;
if (nonFunctionCount !== 0) {
throw new TypeError(`Expected every element in the array to be a function, but found ${
nonFunctionCount === 1 ? 'a non-function value' : 'non-function values'
} in the array: ${arrayToSentence(nonFunctions.map(stringifyFilterResult))}.`);
}
} else {
additionalValidations = [];
}
if (obj === '') {

@@ -224,9 +248,25 @@ return [new TypeError(`${ERROR_MESSAGE}, but got '' (empty string).`)];

if (correctName !== undefined) {
results.push(new Error(
`node-glob doesn't have \`${key}\` option. Probably you meant \`${correctName}\`.`
));
results.push(new Error(`node-glob doesn't have \`${key}\` option. Probably you meant \`${
correctName
}\`.`));
}
}
for (const fn of additionalValidations) {
const additionalResult = fn(obj);
if (additionalResult) {
if (!(additionalResult instanceof Error)) {
throw new TypeError(
'Expected an additional validation function to return an error, but returned ' +
inspect(additionalResult) +
'.'
);
}
results.push(additionalResult);
}
}
return results;
};

4

package.json
{
"name": "validate-glob-opts",
"version": "0.3.0",
"version": "0.4.0",
"description": "Validate glob options",

@@ -26,2 +26,4 @@ "repository": "shinnn/validate-glob-opts",

"dependencies": {
"array-to-sentence": "^1.1.0",
"indexed-filter": "^1.0.0",
"is-plain-obj": "^1.1.0"

@@ -28,0 +30,0 @@ },

@@ -36,5 +36,6 @@ # validate-glob-opts

### validateGlobOpts(*obj*)
### validateGlobOpts(*obj* [, *customValidations*])
*obj*: `Object` ([`glob` options](https://github.com/isaacs/node-glob#options))
*customValidations*: `Array<function>`
Return: `Array` of errors

@@ -83,2 +84,21 @@

#### 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.
```javascript
validateGlobOpts({realPath: true, nodir: true});
/*=> [
Error: node-glob doesn't have `realPath` option. Probably you meant `realpath`.
] */
validateGlobOpts({realPath: true, nodir: true}, [
obj => obj.nodir ? new Error('My app doesn\'t support `nodir` option.') : null
]);
/*=> [
Error: node-glob doesn't have `realPath` option. Probably you meant `realpath`.
TypeError: My app doesn't support `nodir` option.
] */
```
## License

@@ -85,0 +105,0 @@