validate-glob-opts
Advanced tools
Comparing version 0.3.0 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; | ||
}; |
{ | ||
"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 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
13370
236
107
0
3
+ Addedarray-to-sentence@^1.1.0
+ Addedindexed-filter@^1.0.0
+ Addedappend-type@1.0.2(transitive)
+ Addedarray-to-sentence@1.1.0(transitive)
+ Addedindexed-filter@1.0.3(transitive)