@@ -11,24 +11,33 @@ module.exports = function validateOptions(options) {
11
11
return reportError ( 'Options should be an object.' ) ;
12
12
}
13
13
14
- if ( ! _ . isUndefined ( options . order ) && ! _ . isNull ( options . order ) && ! validateOrder ( options . order ) ) {
15
- return reportInvalidOption ( 'order' ) ;
14
+ if ( ! _ . isUndefined ( options . order ) && ! _ . isNull ( options . order ) ) {
15
+ const { isValid, message } = validateOrder ( options . order ) ;
16
+
17
+ if ( ! isValid ) {
18
+ return reportInvalidOption ( 'order' , message ) ;
19
+ }
16
20
}
17
21
18
- if (
19
- ! _ . isUndefined ( options [ 'properties-order' ] ) &&
20
- ! _ . isNull ( options [ 'properties-order' ] ) &&
21
- ! validatePropertiesOrder ( options [ 'properties-order' ] )
22
- ) {
23
- return reportInvalidOption ( 'properties-order' ) ;
22
+ if ( ! _ . isUndefined ( options [ 'properties-order' ] ) && ! _ . isNull ( options [ 'properties-order' ] ) ) {
23
+ const { isValid , message } = validatePropertiesOrder ( options [ 'properties-order' ] ) ;
24
+
25
+ if ( ! isValid ) {
26
+ return reportInvalidOption ( 'properties-order' , message ) ;
27
+ }
24
28
}
25
29
26
30
if (
27
31
! _ . isUndefined ( options [ 'unspecified-properties-position' ] ) &&
28
- ! _ . isNull ( options [ 'unspecified-properties-position' ] ) &&
29
- ! validateUnspecifiedPropertiesPosition ( options [ 'unspecified-properties-position' ] )
32
+ ! _ . isNull ( options [ 'unspecified-properties-position' ] )
30
33
) {
31
- return reportInvalidOption ( 'unspecified-properties-position' ) ;
34
+ const { isValid, message } = validateUnspecifiedPropertiesPosition (
35
+ options [ 'unspecified-properties-position' ]
36
+ ) ;
37
+
38
+ if ( ! isValid ) {
39
+ return reportInvalidOption ( 'unspecified-properties-position' , message ) ;
40
+ }
32
41
}
33
42
34
43
return true ;
@@ -38,46 +47,68 @@ function reportError(errorMessage) {
38
47
return `postcss-sorting: ${ errorMessage } ` ;
39
48
}
40
49
41
- function reportInvalidOption ( optionName ) {
42
- return reportError ( `Invalid "${ optionName } " option value` ) ;
50
+ function reportInvalidOption ( optionName , optionError ) {
51
+ optionError = optionError || 'Invalid value' ;
52
+
53
+ return reportError ( `${ optionName } : ${ optionError } ` ) ;
54
+ }
55
+
56
+ function keywordsList ( keywords ) {
57
+ return keywords . reduce ( function ( accumulator , value , index ) {
58
+ const comma = index === 0 ? '' : ', ' ;
59
+
60
+ return accumulator + comma + value ;
61
+ } , '' ) ;
43
62
}
44
63
45
64
function validateOrder ( options ) {
46
65
// Otherwise, begin checking array options
47
66
if ( ! Array . isArray ( options ) ) {
48
- return false ;
67
+ return {
68
+ isValid : false ,
69
+ message : 'Should be an array' ,
70
+ } ;
49
71
}
50
72
73
+ const keywords = [ 'custom-properties' , 'dollar-variables' , 'declarations' , 'rules' , 'at-rules' ] ;
74
+
51
75
// Every item in the array must be a certain string or an object
52
76
// with a "type" property
53
77
if (
54
78
! options . every ( item => {
55
79
if ( _ . isString ( item ) ) {
56
- return _ . includes (
57
- [ 'custom-properties' , 'dollar-variables' , 'declarations' , 'rules' , 'at-rules' ] ,
58
- item
59
- ) ;
80
+ return _ . includes ( keywords , item ) ;
60
81
}
61
82
62
83
return _ . isPlainObject ( item ) && ! _ . isUndefined ( item . type ) ;
63
84
} )
64
85
) {
65
- return false ;
86
+ return {
87
+ isValid : false ,
88
+ message : `Every item in the array must be an object with a "type" property, or one of keywords: ${ keywordsList (
89
+ keywords
90
+ ) } .`,
91
+ } ;
66
92
}
67
93
68
94
const objectItems = options . filter ( _ . isPlainObject ) ;
95
+ let wrongObjectItem ;
69
96
70
97
if (
71
98
! objectItems . every ( item => {
72
99
let result = true ;
73
100
74
101
if ( item . type !== 'at-rule' && item . type !== 'rule' ) {
102
+ wrongObjectItem = `"type" could be 'at-rule' or 'rule' only` ;
103
+
75
104
return false ;
76
105
}
77
106
78
107
if ( item . type === 'at-rule' ) {
79
108
// if parameter is specified, name should be specified also
80
109
if ( ! _ . isUndefined ( item . parameter ) && _ . isUndefined ( item . name ) ) {
110
+ wrongObjectItem = `"at-rule" with "parameter" should also has a "name"` ;
111
+
81
112
return false ;
82
113
}
83
114
@@ -101,34 +132,64 @@ function validateOrder(options) {
101
132
}
102
133
}
103
134
135
+ if ( ! result ) {
136
+ wrongObjectItem = `Following option is incorrect: ${ JSON . stringify ( item ) } ` ;
137
+ }
138
+
104
139
return result ;
105
140
} )
106
141
) {
107
- return false ;
142
+ return {
143
+ isValid : false ,
144
+ message : wrongObjectItem ,
145
+ } ;
108
146
}
109
147
110
- return true ;
148
+ return {
149
+ isValid : true ,
150
+ } ;
111
151
}
112
152
113
153
function validatePropertiesOrder ( options ) {
114
154
// Return true early if alphabetical
115
155
if ( options === 'alphabetical' ) {
116
- return true ;
156
+ return {
157
+ isValid : true ,
158
+ } ;
117
159
}
118
160
119
161
// Otherwise, begin checking array options
120
162
if ( ! Array . isArray ( options ) ) {
121
- return false ;
163
+ return {
164
+ isValid : false ,
165
+ message : 'Should be an array' ,
166
+ } ;
122
167
}
123
168
124
169
// Every item in the array must be a string
125
170
if ( ! options . every ( item => _ . isString ( item ) ) ) {
126
- return false ;
171
+ return {
172
+ isValid : false ,
173
+ message : 'Array should contain strings only' ,
174
+ } ;
127
175
}
128
176
129
- return true ;
177
+ return {
178
+ isValid : true ,
179
+ } ;
130
180
}
131
181
132
182
function validateUnspecifiedPropertiesPosition ( options ) {
133
- return _ . isString ( options ) && _ . includes ( [ 'top' , 'bottom' , 'bottomAlphabetical' ] , options ) ;
183
+ const keywords = [ 'top' , 'bottom' , 'bottomAlphabetical' ] ;
184
+
185
+ if ( _ . isString ( options ) && _ . includes ( keywords , options ) ) {
186
+ return {
187
+ isValid : true ,
188
+ } ;
189
+ }
190
+
191
+ return {
192
+ isValid : false ,
193
+ message : `Option should be one of the following values: ${ keywordsList ( keywords ) } .` ,
194
+ } ;
134
195
}
0 commit comments