Skip to content

Commit

Permalink
support custom validations
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed May 24, 2017
1 parent 68115de commit aa45b7e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ npm install assert-valid-glob-opts
const assertValidGlobOpts = require('assert-valid-glob-opts');
```

### assertValidGlobOpts(*obj*)
### assertValidGlobOpts(*obj* [, *customValidations*])

*obj*: `Object` ([`glob` options](https://github.com/isaacs/node-glob#options))
*customValidations*: `Array<Function>` (passed to [`validate-glob-opts`](https://github.com/shinnn/validate-glob-opts#validategloboptsobj--customvalidations))

It validates a given object with [validate-glob-opts](https://github.com/shinnn/validate-glob-opts) and throws an error if the value is not valid.

Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
const GlobOptionError = require('glob-option-error');
const validateGlobOpts = require('validate-glob-opts');

module.exports = function assertValidGlobOpts(obj) {
const results = validateGlobOpts(obj);
module.exports = function assertValidGlobOpts(...args) {
const results = validateGlobOpts(...args);

if (results.length === 0) {
return;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"strict"
],
"dependencies": {
"glob-option-error": "^0.1.0",
"validate-glob-opts": "^0.4.0"
"glob-option-error": "^1.0.0",
"validate-glob-opts": "^1.0.0"
},
"devDependencies": {
"@shinnn/eslint-config-node": "^3.0.0",
Expand Down
20 changes: 19 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ test('assertValidGlobOpts()', t => {
'should throw no errors when it takes no arguments.'
);

t.throws(
() => main({}, [], null),
/^TypeError.*Expected 0, 1 or 2 arguments \(\[<object>, <array>]\), but got 3\./,
'should throw no errors when it takes too many arguments.'
);

t.doesNotThrow(
() => main({nodir: true}),
'should throw no errors when it takes a valid glob option.'
);

t.throws(
() => main({cwd: 1}),
/^TypeError: node-glob expected `cwd` option to be a directory path \(string\), but got 1\./,
/^TypeError.*node-glob expected `cwd` option to be a directory path \(string\), but got 1 \(number\)\./,
'should throw an error when it takes an invalid glob option.'
);

Expand All @@ -32,5 +38,17 @@ test('assertValidGlobOpts()', t => {
'should throw a type error when every error in the result is type error.'
);

t.throws(
() => main({x: '!!!err!!!'}, [({x}) => new Error(x)]),
/^Error.*!!!err!!!/,
'should support custom validation parameter.'
);

t.throws(
() => main({}, new WeakMap()),
/^TypeError.*Expected an array of functions, but got a non-array value WeakMap {}\./,
'should invalidate the non-array second aergument.'
);

t.end();
});

0 comments on commit aa45b7e

Please sign in to comment.