forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchemas.lint.js
More file actions
131 lines (118 loc) · 3.45 KB
/
Schemas.lint.js
File metadata and controls
131 lines (118 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"use strict";
const fs = require("fs");
const path = require("path");
const glob = require("glob");
const rootDir = path.resolve(__dirname, "..");
describe("Schemas", () => {
const schemas = glob.sync("schemas/**/*.json", {
cwd: rootDir
});
schemas.forEach(filename => {
describe(filename, () => {
let content;
let fileContent;
let errorWhileParsing;
try {
fileContent = fs.readFileSync(path.resolve(rootDir, filename), "utf-8");
content = JSON.parse(fileContent);
} catch(e) {
errorWhileParsing = e;
}
it("should be parse-able", () => {
if(errorWhileParsing) throw errorWhileParsing;
});
if(content) {
it("should be formated correctly", () => {
expect(fileContent.replace(/\r\n?/g, "\n")).toBe(JSON.stringify(content, 0, 2) + "\n");
});
const arrayProperties = ["oneOf", "anyOf", "allOf"];
const allowedProperties = [
"definitions",
"$ref",
"id",
"items",
"properties",
"additionalProperties",
"type",
"oneOf",
"anyOf",
"allOf",
"absolutePath",
"description",
"enum",
"minLength",
"minimum",
"required",
"uniqueItems",
"minItems",
"minProperties",
"instanceof"
];
const validateProperty = property => {
it("should have description set", () => {
expect(typeof property.description).toBe("string");
expect(property.description.length).toBeGreaterThan(1);
});
};
const walker = item => {
it("should only use allowed schema properties", () => {
const otherProperties = Object.keys(item).filter(p => allowedProperties.indexOf(p) < 0);
if(otherProperties.length > 0) {
throw new Error(`The properties ${otherProperties.join(", ")} are not allowed to use`);
// When allowing more properties make sure to add nice error messages for them in WebpackOptionsValidationError
}
});
if(Object.keys(item).indexOf("$ref") >= 0) {
it("should not have other properties next to $ref", () => {
const otherProperties = Object.keys(item).filter(p => p !== "$ref");
if(otherProperties.length > 0) {
throw new Error(`When using $ref not other properties are possible (${otherProperties.join(", ")})`);
}
});
}
arrayProperties.forEach(prop => {
if(prop in item) {
describe(prop, () => {
item[prop].forEach(walker);
});
}
});
if("items" in item) {
describe("items", () => {
if(Object.keys(item).join() !== "$ref") {
validateProperty(item.items);
}
walker(item.items);
});
}
if("definitions" in item) {
Object.keys(item.definitions).forEach(name => {
describe(`#${name}`, () => {
walker(item.definitions[name]);
});
});
}
if("properties" in item) {
it("should have additionalProperties set to some value when descriping properties", () => {
expect(item.additionalProperties).toBeDefined();
});
Object.keys(item.properties).forEach(name => {
describe(`> '${name}'`, () => {
const property = item.properties[name];
validateProperty(property);
walker(property);
});
});
}
if(typeof item.additionalProperties === "object") {
describe("properties", () => {
validateProperty(item.additionalProperties);
walker(item.additionalProperties);
});
}
};
walker(content);
}
});
});
});