77const WebpackError = require ( "./WebpackError" ) ;
88const webpackOptionsSchema = require ( "../schemas/webpackOptionsSchema.json" ) ;
99
10- const getSchemaPart = ( path , parents , additionalPath ) => {
11- parents = parents || 0 ;
12- path = path . split ( "/" ) ;
13- path = path . slice ( 0 , path . length - parents ) ;
14- if ( additionalPath ) {
15- additionalPath = additionalPath . split ( "/" ) ;
16- path = path . concat ( additionalPath ) ;
17- }
18- let schemaPart = webpackOptionsSchema ;
19- for ( let i = 1 ; i < path . length ; i ++ ) {
20- const inner = schemaPart [ path [ i ] ] ;
21- if ( inner )
22- schemaPart = inner ;
23- }
24- return schemaPart ;
25- } ;
26-
27- const getSchemaPartText = ( schemaPart , additionalPath ) => {
28- if ( additionalPath ) {
29- for ( let i = 0 ; i < additionalPath . length ; i ++ ) {
30- const inner = schemaPart [ additionalPath [ i ] ] ;
31- if ( inner )
32- schemaPart = inner ;
33- }
34- }
35- while ( schemaPart . $ref ) schemaPart = getSchemaPart ( schemaPart . $ref ) ;
36- let schemaText = WebpackOptionsValidationError . formatSchema ( schemaPart ) ;
37- if ( schemaPart . description )
38- schemaText += `\n${ schemaPart . description } ` ;
39- return schemaText ;
40- } ;
10+ const indent = require ( "./util/indent" ) ;
11+ const getSchemaPartText = require ( "./util/getSchemaPartText" ) ;
4112
42- const indent = ( str , prefix , firstLine ) => {
43- if ( firstLine ) {
44- return prefix + str . replace ( / \n (? ! $ ) / g, "\n" + prefix ) ;
45- } else {
46- return str . replace ( / \n (? ! $ ) / g, `\n${ prefix } ` ) ;
47- }
48- } ;
13+ const getOptionsSchemaPartText = ( schema , additionalPath ) =>
14+ getSchemaPartText ( webpackOptionsSchema , schema , additionalPath ) ;
4915
5016class WebpackOptionsValidationError extends WebpackError {
5117 constructor ( validationErrors ) {
@@ -60,59 +26,10 @@ class WebpackOptionsValidationError extends WebpackError {
6026 Error . captureStackTrace ( this , this . constructor ) ;
6127 }
6228
63- static formatSchema ( schema , prevSchemas ) {
64- prevSchemas = prevSchemas || [ ] ;
65-
66- const formatInnerSchema = ( innerSchema , addSelf ) => {
67- if ( ! addSelf ) return WebpackOptionsValidationError . formatSchema ( innerSchema , prevSchemas ) ;
68- if ( prevSchemas . indexOf ( innerSchema ) >= 0 ) return "(recursive)" ;
69- return WebpackOptionsValidationError . formatSchema ( innerSchema , prevSchemas . concat ( schema ) ) ;
70- } ;
71-
72- if ( schema . type === "string" ) {
73- if ( schema . minLength === 1 )
74- return "non-empty string" ;
75- else if ( schema . minLength > 1 )
76- return `string (min length ${ schema . minLength } )` ;
77- return "string" ;
78- } else if ( schema . type === "boolean" ) {
79- return "boolean" ;
80- } else if ( schema . type === "number" ) {
81- return "number" ;
82- } else if ( schema . type === "object" ) {
83- if ( schema . properties ) {
84- const required = schema . required || [ ] ;
85- return `object { ${ Object . keys ( schema . properties ) . map ( property => {
86- if ( required . indexOf ( property ) < 0 ) return property + "?" ;
87- return property ;
88- } ) . concat ( schema . additionalProperties ? [ "..." ] : [ ] ) . join ( ", " ) } }`;
89- }
90- if ( schema . additionalProperties ) {
91- return `object { <key>: ${ formatInnerSchema ( schema . additionalProperties ) } }` ;
92- }
93- return "object" ;
94- } else if ( schema . type === "array" ) {
95- return `[${ formatInnerSchema ( schema . items ) } ]` ;
96- }
97-
98- switch ( schema . instanceof ) {
99- case "Function" :
100- return "function" ;
101- case "RegExp" :
102- return "RegExp" ;
103- }
104- if ( schema . $ref ) return formatInnerSchema ( getSchemaPart ( schema . $ref ) , true ) ;
105- if ( schema . allOf ) return schema . allOf . map ( formatInnerSchema ) . join ( " & " ) ;
106- if ( schema . oneOf ) return schema . oneOf . map ( formatInnerSchema ) . join ( " | " ) ;
107- if ( schema . anyOf ) return schema . anyOf . map ( formatInnerSchema ) . join ( " | " ) ;
108- if ( schema . enum ) return schema . enum . map ( item => JSON . stringify ( item ) ) . join ( " | " ) ;
109- return JSON . stringify ( schema , 0 , 2 ) ;
110- }
111-
11229 static formatValidationError ( err ) {
11330 const dataPath = `configuration${ err . dataPath } ` ;
11431 if ( err . keyword === "additionalProperties" ) {
115- const baseMessage = `${ dataPath } has an unknown property '${ err . params . additionalProperty } '. These properties are valid:\n${ getSchemaPartText ( err . parentSchema ) } ` ;
32+ const baseMessage = `${ dataPath } has an unknown property '${ err . params . additionalProperty } '. These properties are valid:\n${ getOptionsSchemaPartText ( err . parentSchema ) } ` ;
11633 if ( ! err . dataPath ) {
11734 switch ( err . params . additionalProperty ) {
11835 case "debug" :
@@ -143,18 +60,18 @@ class WebpackOptionsValidationError extends WebpackError {
14360 return baseMessage ;
14461 } else if ( err . keyword === "oneOf" || err . keyword === "anyOf" ) {
14562 if ( err . children && err . children . length > 0 ) {
146- return `${ dataPath } should be one of these:\n${ getSchemaPartText ( err . parentSchema ) } \n` +
63+ return `${ dataPath } should be one of these:\n${ getOptionsSchemaPartText ( err . parentSchema ) } \n` +
14764 `Details:\n${ err . children . map ( err => " * " + indent ( WebpackOptionsValidationError . formatValidationError ( err ) , " " , false ) ) . join ( "\n" ) } ` ;
14865 }
149- return `${ dataPath } should be one of these:\n${ getSchemaPartText ( err . parentSchema ) } ` ;
66+ return `${ dataPath } should be one of these:\n${ getOptionsSchemaPartText ( err . parentSchema ) } ` ;
15067
15168 } else if ( err . keyword === "enum" ) {
15269 if ( err . parentSchema && err . parentSchema . enum && err . parentSchema . enum . length === 1 ) {
153- return `${ dataPath } should be ${ getSchemaPartText ( err . parentSchema ) } ` ;
70+ return `${ dataPath } should be ${ getOptionsSchemaPartText ( err . parentSchema ) } ` ;
15471 }
155- return `${ dataPath } should be one of these:\n${ getSchemaPartText ( err . parentSchema ) } ` ;
72+ return `${ dataPath } should be one of these:\n${ getOptionsSchemaPartText ( err . parentSchema ) } ` ;
15673 } else if ( err . keyword === "allOf" ) {
157- return `${ dataPath } should be:\n${ getSchemaPartText ( err . parentSchema ) } ` ;
74+ return `${ dataPath } should be:\n${ getOptionsSchemaPartText ( err . parentSchema ) } ` ;
15875 } else if ( err . keyword === "type" ) {
15976 switch ( err . params . type ) {
16077 case "object" :
@@ -166,14 +83,14 @@ class WebpackOptionsValidationError extends WebpackError {
16683 case "number" :
16784 return `${ dataPath } should be a number.` ;
16885 case "array" :
169- return `${ dataPath } should be an array:\n${ getSchemaPartText ( err . parentSchema ) } ` ;
86+ return `${ dataPath } should be an array:\n${ getOptionsSchemaPartText ( err . parentSchema ) } ` ;
17087 }
171- return `${ dataPath } should be ${ err . params . type } :\n${ getSchemaPartText ( err . parentSchema ) } ` ;
88+ return `${ dataPath } should be ${ err . params . type } :\n${ getOptionsSchemaPartText ( err . parentSchema ) } ` ;
17289 } else if ( err . keyword === "instanceof" ) {
173- return `${ dataPath } should be an instance of ${ getSchemaPartText ( err . parentSchema ) } .` ;
90+ return `${ dataPath } should be an instance of ${ getOptionsSchemaPartText ( err . parentSchema ) } .` ;
17491 } else if ( err . keyword === "required" ) {
17592 const missingProperty = err . params . missingProperty . replace ( / ^ \. / , "" ) ;
176- return `${ dataPath } misses the property '${ missingProperty } '.\n${ getSchemaPartText ( err . parentSchema , [ "properties" , missingProperty ] ) } ` ;
93+ return `${ dataPath } misses the property '${ missingProperty } '.\n${ getOptionsSchemaPartText ( err . parentSchema , [ "properties" , missingProperty ] ) } ` ;
17794 } else if ( err . keyword === "minLength" || err . keyword === "minItems" ) {
17895 if ( err . params . limit === 1 )
17996 return `${ dataPath } should not be empty.` ;
@@ -188,7 +105,7 @@ class WebpackOptionsValidationError extends WebpackError {
188105 return baseMessage ;
189106 } else {
190107 // eslint-disable-line no-fallthrough
191- return `${ dataPath } ${ err . message } (${ JSON . stringify ( err , 0 , 2 ) } ).\n${ getSchemaPartText ( err . parentSchema ) } ` ;
108+ return `${ dataPath } ${ err . message } (${ JSON . stringify ( err , 0 , 2 ) } ).\n${ getOptionsSchemaPartText ( err . parentSchema ) } ` ;
192109 }
193110 }
194111}
0 commit comments