99// Requirements
1010//------------------------------------------------------------------------------
1111
12- const schemaValidator = require ( "is-my-json-valid " ) ,
12+ const ajv = require ( "../util/ajv " ) ,
1313 configSchema = require ( "../../conf/config-schema.js" ) ,
1414 util = require ( "util" ) ;
1515
@@ -20,6 +20,7 @@ const validators = {
2020//------------------------------------------------------------------------------
2121// Private
2222//------------------------------------------------------------------------------
23+ let validateSchema ;
2324
2425/**
2526 * Gets a complete options schema for a rule.
@@ -79,15 +80,15 @@ function validateRuleSchema(id, localOptions, rulesContext) {
7980 const schema = getRuleOptionsSchema ( id , rulesContext ) ;
8081
8182 if ( ! validators . rules [ id ] && schema ) {
82- validators . rules [ id ] = schemaValidator ( schema , { verbose : true } ) ;
83+ validators . rules [ id ] = ajv . compile ( schema ) ;
8384 }
8485
8586 const validateRule = validators . rules [ id ] ;
8687
8788 if ( validateRule ) {
8889 validateRule ( localOptions ) ;
8990 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 ( "" ) ) ;
9192 }
9293 }
9394}
@@ -158,19 +159,23 @@ function validateRules(rulesConfig, source, rulesContext) {
158159 * @returns {string } Formatted error message
159160 */
160161function formatErrors ( errors ) {
161-
162162 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 } "` ;
165167 }
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 ) ;
170172
171173 return `Property "${ formattedField } " is the wrong type (expected ${ formattedExpectedType } but got \`${ formattedValue } \`)` ;
172174 }
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 ) } ` ;
174179 } ) . map ( message => `\t- ${ message } .\n` ) . join ( "" ) ;
175180}
176181
@@ -181,10 +186,10 @@ function formatErrors(errors) {
181186 * @returns {void }
182187 */
183188function validateConfigSchema ( config , source ) {
184- const validator = schemaValidator ( configSchema , { verbose : true } ) ;
189+ validateSchema = validateSchema || ajv . compile ( configSchema ) ;
185190
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 ) } ` ) ;
188193 }
189194}
190195
0 commit comments