9
9
// Requirements
10
10
//------------------------------------------------------------------------------
11
11
12
- const schemaValidator = require ( "is-my-json-valid " ) ,
12
+ const ajv = require ( "../util/ajv " ) ,
13
13
configSchema = require ( "../../conf/config-schema.js" ) ,
14
14
util = require ( "util" ) ;
15
15
@@ -20,6 +20,7 @@ const validators = {
20
20
//------------------------------------------------------------------------------
21
21
// Private
22
22
//------------------------------------------------------------------------------
23
+ let validateSchema ;
23
24
24
25
/**
25
26
* Gets a complete options schema for a rule.
@@ -79,15 +80,15 @@ function validateRuleSchema(id, localOptions, rulesContext) {
79
80
const schema = getRuleOptionsSchema ( id , rulesContext ) ;
80
81
81
82
if ( ! validators . rules [ id ] && schema ) {
82
- validators . rules [ id ] = schemaValidator ( schema , { verbose : true } ) ;
83
+ validators . rules [ id ] = ajv . compile ( schema ) ;
83
84
}
84
85
85
86
const validateRule = validators . rules [ id ] ;
86
87
87
88
if ( validateRule ) {
88
89
validateRule ( localOptions ) ;
89
90
if ( validateRule . errors ) {
90
- throw new Error ( validateRule . errors . map ( error => `\tValue "${ error . value } " ${ error . message } .\n` ) . join ( "" ) ) ;
91
+ throw new Error ( validateRule . errors . map ( error => `\tValue "${ error . data } " ${ error . message } .\n` ) . join ( "" ) ) ;
91
92
}
92
93
}
93
94
}
@@ -158,19 +159,23 @@ function validateRules(rulesConfig, source, rulesContext) {
158
159
* @returns {string } Formatted error message
159
160
*/
160
161
function formatErrors ( errors ) {
161
-
162
162
return errors . map ( error => {
163
- if ( error . message === "has additional properties" ) {
164
- return `Unexpected top-level property "${ error . value . replace ( / ^ d a t a \. / , "" ) } "` ;
163
+ if ( error . keyword === "additionalProperties" ) {
164
+ const formattedPropertyPath = error . dataPath . length ? `${ error . dataPath . slice ( 1 ) } .${ error . params . additionalProperty } ` : error . params . additionalProperty ;
165
+
166
+ return `Unexpected top-level property "${ formattedPropertyPath } "` ;
165
167
}
166
- if ( error . message === "is the wrong type" ) {
167
- const formattedField = error . field . replace ( / ^ d a t a \. / , "" ) ;
168
- const formattedExpectedType = typeof error . type === "string" ? error . type : error . type . join ( "/" ) ;
169
- const formattedValue = JSON . stringify ( error . value ) ;
168
+ if ( error . keyword === "type" ) {
169
+ const formattedField = error . dataPath . slice ( 1 ) ;
170
+ const formattedExpectedType = Array . isArray ( error . schema ) ? error . schema . join ( "/" ) : error . schema ;
171
+ const formattedValue = JSON . stringify ( error . data ) ;
170
172
171
173
return `Property "${ formattedField } " is the wrong type (expected ${ formattedExpectedType } but got \`${ formattedValue } \`)` ;
172
174
}
173
- return `"${ error . field . replace ( / ^ ( d a t a \. ) / , "" ) } " ${ error . message } . Value: ${ JSON . stringify ( error . value ) } ` ;
175
+
176
+ const field = error . dataPath [ 0 ] === "." ? error . dataPath . slice ( 1 ) : error . dataPath ;
177
+
178
+ return `"${ field } " ${ error . message } . Value: ${ JSON . stringify ( error . data ) } ` ;
174
179
} ) . map ( message => `\t- ${ message } .\n` ) . join ( "" ) ;
175
180
}
176
181
@@ -181,10 +186,10 @@ function formatErrors(errors) {
181
186
* @returns {void }
182
187
*/
183
188
function validateConfigSchema ( config , source ) {
184
- const validator = schemaValidator ( configSchema , { verbose : true } ) ;
189
+ validateSchema = validateSchema || ajv . compile ( configSchema ) ;
185
190
186
- if ( ! validator ( config ) ) {
187
- throw new Error ( `${ source } :\n\tESLint configuration is invalid:\n${ formatErrors ( validator . errors ) } ` ) ;
191
+ if ( ! validateSchema ( config ) ) {
192
+ throw new Error ( `${ source } :\n\tESLint configuration is invalid:\n${ formatErrors ( validateSchema . errors ) } ` ) ;
188
193
}
189
194
}
190
195
0 commit comments