Skip to content

Commit fbad067

Browse files
committed
Revert changes to WebpackOptionsValidationError
1 parent 744dbe9 commit fbad067

1 file changed

Lines changed: 98 additions & 15 deletions

File tree

‎lib/WebpackOptionsValidationError.js‎

Lines changed: 98 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,45 @@
77
const WebpackError = require("./WebpackError");
88
const webpackOptionsSchema = require("../schemas/webpackOptionsSchema.json");
99

10-
const indent = require("./util/indent");
11-
const getSchemaPartText = require("./util/getSchemaPartText");
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+
};
1241

13-
const getOptionsSchemaPartText = (schema, additionalPath) =>
14-
getSchemaPartText(webpackOptionsSchema, schema, additionalPath);
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+
};
1549

1650
class WebpackOptionsValidationError extends WebpackError {
1751
constructor(validationErrors) {
@@ -26,10 +60,59 @@ class WebpackOptionsValidationError extends WebpackError {
2660
Error.captureStackTrace(this, this.constructor);
2761
}
2862

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+
29112
static formatValidationError(err) {
30113
const dataPath = `configuration${err.dataPath}`;
31114
if(err.keyword === "additionalProperties") {
32-
const baseMessage = `${dataPath} has an unknown property '${err.params.additionalProperty}'. These properties are valid:\n${getOptionsSchemaPartText(err.parentSchema)}`;
115+
const baseMessage = `${dataPath} has an unknown property '${err.params.additionalProperty}'. These properties are valid:\n${getSchemaPartText(err.parentSchema)}`;
33116
if(!err.dataPath) {
34117
switch(err.params.additionalProperty) {
35118
case "debug":
@@ -60,18 +143,18 @@ class WebpackOptionsValidationError extends WebpackError {
60143
return baseMessage;
61144
} else if(err.keyword === "oneOf" || err.keyword === "anyOf") {
62145
if(err.children && err.children.length > 0) {
63-
return `${dataPath} should be one of these:\n${getOptionsSchemaPartText(err.parentSchema)}\n` +
146+
return `${dataPath} should be one of these:\n${getSchemaPartText(err.parentSchema)}\n` +
64147
`Details:\n${err.children.map(err => " * " + indent(WebpackOptionsValidationError.formatValidationError(err), " ", false)).join("\n")}`;
65148
}
66-
return `${dataPath} should be one of these:\n${getOptionsSchemaPartText(err.parentSchema)}`;
149+
return `${dataPath} should be one of these:\n${getSchemaPartText(err.parentSchema)}`;
67150

68151
} else if(err.keyword === "enum") {
69152
if(err.parentSchema && err.parentSchema.enum && err.parentSchema.enum.length === 1) {
70-
return `${dataPath} should be ${getOptionsSchemaPartText(err.parentSchema)}`;
153+
return `${dataPath} should be ${getSchemaPartText(err.parentSchema)}`;
71154
}
72-
return `${dataPath} should be one of these:\n${getOptionsSchemaPartText(err.parentSchema)}`;
155+
return `${dataPath} should be one of these:\n${getSchemaPartText(err.parentSchema)}`;
73156
} else if(err.keyword === "allOf") {
74-
return `${dataPath} should be:\n${getOptionsSchemaPartText(err.parentSchema)}`;
157+
return `${dataPath} should be:\n${getSchemaPartText(err.parentSchema)}`;
75158
} else if(err.keyword === "type") {
76159
switch(err.params.type) {
77160
case "object":
@@ -83,14 +166,14 @@ class WebpackOptionsValidationError extends WebpackError {
83166
case "number":
84167
return `${dataPath} should be a number.`;
85168
case "array":
86-
return `${dataPath} should be an array:\n${getOptionsSchemaPartText(err.parentSchema)}`;
169+
return `${dataPath} should be an array:\n${getSchemaPartText(err.parentSchema)}`;
87170
}
88-
return `${dataPath} should be ${err.params.type}:\n${getOptionsSchemaPartText(err.parentSchema)}`;
171+
return `${dataPath} should be ${err.params.type}:\n${getSchemaPartText(err.parentSchema)}`;
89172
} else if(err.keyword === "instanceof") {
90-
return `${dataPath} should be an instance of ${getOptionsSchemaPartText(err.parentSchema)}.`;
173+
return `${dataPath} should be an instance of ${getSchemaPartText(err.parentSchema)}.`;
91174
} else if(err.keyword === "required") {
92175
const missingProperty = err.params.missingProperty.replace(/^\./, "");
93-
return `${dataPath} misses the property '${missingProperty}'.\n${getOptionsSchemaPartText(err.parentSchema, ["properties", missingProperty])}`;
176+
return `${dataPath} misses the property '${missingProperty}'.\n${getSchemaPartText(err.parentSchema, ["properties", missingProperty])}`;
94177
} else if(err.keyword === "minLength" || err.keyword === "minItems") {
95178
if(err.params.limit === 1)
96179
return `${dataPath} should not be empty.`;
@@ -105,7 +188,7 @@ class WebpackOptionsValidationError extends WebpackError {
105188
return baseMessage;
106189
} else {
107190
// eslint-disable-line no-fallthrough
108-
return `${dataPath} ${err.message} (${JSON.stringify(err, 0, 2)}).\n${getOptionsSchemaPartText(err.parentSchema)}`;
191+
return `${dataPath} ${err.message} (${JSON.stringify(err, 0, 2)}).\n${getSchemaPartText(err.parentSchema)}`;
109192
}
110193
}
111194
}

0 commit comments

Comments
 (0)