Skip to content

Commit 0a4abde

Browse files
mightyalekseyAlexey Litvinov
authored andcommitted
implemented validation
1 parent 3292d25 commit 0a4abde

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lib/validate.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
const difference = require('lodash').difference;
2+
const forEach = require('lodash').forEach;
3+
const keys = require('lodash').keys;
4+
15
const rules = {
26
// hook
37
extensions: 'array|string',
@@ -14,4 +18,25 @@ const rules = {
1418
rootDir: 'string',
1519
};
1620

17-
module.exports = function validate(options) {}
21+
const tests = {
22+
array: require('lodash').isArray,
23+
function: require('lodash').isFunction,
24+
string: require('lodash').isString,
25+
};
26+
27+
module.exports = function validate(options) {
28+
const unknownOptions = difference(keys(options), keys(rules));
29+
if (unknownOptions.length) {
30+
throw new Error(`unknown arguments: ${unknownOptions.join(', ')}.`);
31+
}
32+
33+
forEach(rules, (types, rule) => {
34+
if (typeof options[rule] === 'undefined') {
35+
return;
36+
}
37+
38+
if (!types.split('|').some(type => tests[type](options[rule]))) {
39+
throw new TypeError(`should specify ${types} as ${rule}`);
40+
}
41+
});
42+
}

test/lib/validate.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const validate = require('../../lib/validate');
2+
3+
suite('lib/validate', () => {
4+
test('should thrown an error for the option with multiple types if wrong type specified', () => {
5+
assert.throws(() => validate({extensions: null}, TypeError));
6+
});
7+
8+
test('should thrown an error for the option with single type if wrong type specified', () => {
9+
assert.throws(() => validate({preprocessCss: ''}, TypeError));
10+
});
11+
12+
test('should NOT throw an error for the valid type', () => {
13+
assert.doesNotThrow(() => validate({preprocessCss: function () {}}));
14+
});
15+
16+
test('should throw an error if unknown options are specified', () => {
17+
assert.throws(() => validate({a: '', b: ''}), Error);
18+
});
19+
});

0 commit comments

Comments
 (0)