Skip to content

Commit 41700ee

Browse files
committed
Add WebpackPluginValidationError
1 parent b50d4cf commit 41700ee

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"use strict";
2+
3+
const WebpackError = require("./WebpackError");
4+
5+
const indent = require("./util/indent");
6+
const getSchemaPartText = require("./util/getSchemaPartText");
7+
8+
const kebabCase = (str) => str.replace(/(\w|\s+)([A-Z])/g, (match, p1, p2) => p1.trim() + "-" + p2).toLowerCase();
9+
10+
class WebpackPluginValidationError extends WebpackError {
11+
constructor(validationErrors, pluginSchema) {
12+
super();
13+
14+
this.name = "WebpackPluginValidationError";
15+
this.message =
16+
"Invalid plugin options. " + pluginSchema.id + " has been initialised using an invalid options argument.\n" +
17+
validationErrors.map(err => " - " + indent(WebpackPluginValidationError.formatValidationError(pluginSchema, err), " ", false)).join("\n") +
18+
"\n\nMore info at: https://webpack.js.org/plugins/" + (pluginSchema.slug || kebabCase(pluginSchema.id));
19+
20+
this.pluginSchema = pluginSchema;
21+
this.validationErrors = validationErrors;
22+
23+
Error.captureStackTrace(this, this.constructor);
24+
}
25+
26+
static formatValidationError(schema, err) {
27+
const dataPath = `options${err.dataPath}`;
28+
const getPluginSchemaPartText = (schemaPart, additionalPath) => getSchemaPartText(schema, schemaPart, additionalPath);
29+
30+
if(err.keyword === "additionalProperties") {
31+
return `${dataPath} has an unknown property '${err.params.additionalProperty}'. These properties are valid:\n${getPluginSchemaPartText(err.parentSchema)}`;
32+
} else if(err.keyword === "oneOf" || err.keyword === "anyOf") {
33+
if(err.children && err.children.length > 0) {
34+
return `${dataPath} should be one of these:\n${getPluginSchemaPartText(err.parentSchema)}\n` +
35+
`Details:\n${err.children.map(err => " * " + indent(WebpackPluginValidationError.formatValidationError(schema, err), " ", false)).join("\n")}`;
36+
}
37+
return `${dataPath} should be one of these:\n${getPluginSchemaPartText(err.parentSchema)}`;
38+
} else if(err.keyword === "enum") {
39+
if(err.parentSchema && err.parentSchema.enum && err.parentSchema.enum.length === 1) {
40+
return `${dataPath} should be ${getPluginSchemaPartText(err.parentSchema)}`;
41+
}
42+
return `${dataPath} should be one of these:\n${getPluginSchemaPartText(err.parentSchema)}`;
43+
} else if(err.keyword === "allOf") {
44+
return `${dataPath} should be:\n${getPluginSchemaPartText(err.parentSchema)}`;
45+
} else if(err.keyword === "type") {
46+
switch(err.params.type) {
47+
case "object":
48+
return `${dataPath} should be an object.`;
49+
case "string":
50+
return `${dataPath} should be a string.`;
51+
case "boolean":
52+
return `${dataPath} should be a boolean.`;
53+
case "number":
54+
return `${dataPath} should be a number.`;
55+
case "array":
56+
return `${dataPath} should be an array:\n${getPluginSchemaPartText(err.parentSchema)}`;
57+
}
58+
return `${dataPath} should be ${err.params.type}:\n${getPluginSchemaPartText(err.parentSchema)}`;
59+
} else if(err.keyword === "instanceof") {
60+
return `${dataPath} should be an instance of ${getPluginSchemaPartText(err.parentSchema)}.`;
61+
} else if(err.keyword === "required") {
62+
const missingProperty = err.params.missingProperty.replace(/^\./, "");
63+
return `${dataPath} misses the property '${missingProperty}'.\n${getPluginSchemaPartText(err.parentSchema, ["properties", missingProperty])}`;
64+
} else if(err.keyword === "minLength" || err.keyword === "minItems") {
65+
if(err.params.limit === 1) return `${dataPath} should not be empty.`;
66+
else return `${dataPath} ${err.message}`;
67+
} else if(err.keyword === "absolutePath") {
68+
return `${dataPath}: ${err.message}`;
69+
} else if(err.keyword === "minimum" || err.keyword === "maximum") {
70+
return `${dataPath} ${err.message}`;
71+
} else {
72+
// eslint-disable-line no-fallthrough
73+
return `${dataPath} ${err.message} (${JSON.stringify(err, 0, 2)}).\n${getPluginSchemaPartText(err.parentSchema)}`;
74+
}
75+
}
76+
}
77+
78+
module.exports = WebpackPluginValidationError;

0 commit comments

Comments
 (0)