Skip to content

Commit b50d4cf

Browse files
committed
Move common schema functions to util
1 parent 9ee5532 commit b50d4cf

5 files changed

Lines changed: 121 additions & 98 deletions

File tree

‎lib/WebpackOptionsValidationError.js‎

Lines changed: 15 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,11 @@
77
const WebpackError = require("./WebpackError");
88
const 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

5016
class 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
}

‎lib/util/formatSchema.js‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"use strict";
2+
3+
const getSchemaPart = require("./getSchemaPart");
4+
5+
const formatSchema = (rootSchema, schema, prevSchemas) => {
6+
prevSchemas = prevSchemas || [];
7+
8+
const formatInnerSchema = (innerSchema, addSelf) => {
9+
if(!addSelf) return formatSchema(rootSchema, innerSchema, prevSchemas);
10+
if(prevSchemas.indexOf(innerSchema) >= 0) return "(recursive)";
11+
return formatSchema(rootSchema, innerSchema, prevSchemas.concat(schema));
12+
};
13+
14+
if(schema.type === "string") {
15+
if(schema.minLength === 1)
16+
return "non-empty string";
17+
else if(schema.minLength > 1)
18+
return `string (min length ${schema.minLength})`;
19+
return "string";
20+
} else if(schema.type === "boolean") {
21+
return "boolean";
22+
} else if(schema.type === "number") {
23+
return "number";
24+
} else if(schema.type === "object") {
25+
if(schema.properties) {
26+
const required = schema.required || [];
27+
return `object { ${Object.keys(schema.properties).map(property => {
28+
if(required.indexOf(property) < 0) return property + "?";
29+
return property;
30+
}).concat(schema.additionalProperties ? ["..."] : []).join(", ")} }`;
31+
}
32+
if(schema.additionalProperties) {
33+
return `object { <key>: ${formatInnerSchema(schema.additionalProperties)} }`;
34+
}
35+
return "object";
36+
} else if(schema.type === "array") {
37+
return `[${formatInnerSchema(schema.items)}]`;
38+
}
39+
40+
switch(schema.instanceof) {
41+
case "Function":
42+
return "function";
43+
case "RegExp":
44+
return "RegExp";
45+
}
46+
if(schema.$ref) return formatInnerSchema(getSchemaPart(rootSchema, schema.$ref), true);
47+
if(schema.allOf) return schema.allOf.map(formatInnerSchema).join(" & ");
48+
if(schema.oneOf) return schema.oneOf.map(formatInnerSchema).join(" | ");
49+
if(schema.anyOf) return schema.anyOf.map(formatInnerSchema).join(" | ");
50+
if(schema.enum) return schema.enum.map(item => JSON.stringify(item)).join(" | ");
51+
return JSON.stringify(schema, 0, 2);
52+
};
53+
54+
module.exports = formatSchema;

‎lib/util/getSchemaPart.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
3+
const getSchemaPart = (schema, path, parents, additionalPath) => {
4+
parents = parents || 0;
5+
path = path.split("/");
6+
path = path.slice(0, path.length - parents);
7+
if(additionalPath) {
8+
additionalPath = additionalPath.split("/");
9+
path = path.concat(additionalPath);
10+
}
11+
let schemaPart = schema;
12+
for(let i = 1; i < path.length; i++) {
13+
const inner = schemaPart[path[i]];
14+
if(inner)
15+
schemaPart = inner;
16+
}
17+
return schemaPart;
18+
};
19+
20+
module.exports = getSchemaPart;

‎lib/util/getSchemaPartText.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
3+
const formatSchema = require("./formatSchema");
4+
const getSchemaPart = require("./getSchemaPart");
5+
6+
const getSchemaPartText = (rootSchema, schemaPart, additionalPath) => {
7+
if(additionalPath) {
8+
for(let i = 0; i < additionalPath.length; i++) {
9+
const inner = schemaPart[additionalPath[i]];
10+
if(inner)
11+
schemaPart = inner;
12+
}
13+
}
14+
while(schemaPart.$ref) schemaPart = getSchemaPart(rootSchema, schemaPart.$ref);
15+
let schemaText = formatSchema(rootSchema, schemaPart);
16+
if(schemaPart.description)
17+
schemaText += `\n${schemaPart.description}`;
18+
return schemaText;
19+
};
20+
21+
module.exports = getSchemaPartText;

‎lib/util/indent.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
3+
const indent = (str, prefix, firstLine) => {
4+
if(firstLine) {
5+
return prefix + str.replace(/\n(?!$)/g, "\n" + prefix);
6+
} else {
7+
return str.replace(/\n(?!$)/g, `\n${prefix}`);
8+
}
9+
};
10+
11+
module.exports = indent;

0 commit comments

Comments
 (0)