File tree Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change
1
+ const difference = require ( 'lodash' ) . difference ;
2
+ const forEach = require ( 'lodash' ) . forEach ;
3
+ const keys = require ( 'lodash' ) . keys ;
4
+
1
5
const rules = {
2
6
// hook
3
7
extensions : 'array|string' ,
@@ -14,4 +18,25 @@ const rules = {
14
18
rootDir : 'string' ,
15
19
} ;
16
20
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
+ }
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments