-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-content.ts
More file actions
346 lines (306 loc) · 11.5 KB
/
validate-content.ts
File metadata and controls
346 lines (306 loc) · 11.5 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env tsx
/**
* Content Validator Script
*
* This script validates all content files in the `content/pages` and `content/shared` directories
* against the specifications defined in `src/layouts` and `src/components/widgets`.
*
* Usage:
* `npm run validate-content`
*
* Options:
* `--debug`: Show all validated files and other detailed information.
* `--quiet`: Only show errors.
*/
import path from 'path';
import fs from 'fs';
import { glob } from 'glob';
import yaml from 'js-yaml';
import { exit } from 'process';
const CWD = process.cwd();
const SPEC_FILE = path.join(CWD, 'dev/03_content_ops/content.spec.yaml');
const CONTENT_DIRECTORIES = ['content/pages', 'content/shared'];
/**
* Loads the specification file.
* @returns A promise that resolves to the specification object.
*/
async function loadSpecification(): Promise<unknown> {
const content = await fs.promises.readFile(SPEC_FILE, 'utf-8');
return yaml.load(content);
}
/**
* Loads all content files from the predefined content directories.
* @returns A promise that resolves to an array of content file paths.
*/
async function loadContentFiles(): Promise<string[]> {
const contentFiles: string[] = [];
for (const dir of CONTENT_DIRECTORIES) {
const files = await glob(path.join(CWD, dir, '**', '*.yaml'));
contentFiles.push(...files);
}
return contentFiles;
}
/**
* Resolves a $ref pointer to its schema definition.
* @param ref The reference string (e.g., '#/definitions/Image').
* @param specification The root specification object.
* @returns The resolved schema, or undefined if not found.
*/
function getSchema(ref: string, specification: Record<string, unknown>): unknown {
const parts = ref.split('/').slice(1);
let schema: unknown = specification;
for (const part of parts) {
if (schema && typeof schema === 'object' && part in schema) {
schema = (schema as Record<string, unknown>)[part];
} else {
return undefined;
}
}
return schema;
}
function getAllowedProperties(
schema: Record<string, unknown>,
specification: Record<string, unknown>
): Set<string> {
const properties = new Set<string>();
if (schema.$ref && typeof schema.$ref === 'string') {
schema = getSchema(schema.$ref, specification) as Record<string, unknown>;
}
if (schema.properties && typeof schema.properties === 'object') {
Object.keys(schema.properties).forEach((prop) => properties.add(prop));
}
if (schema.allOf && Array.isArray(schema.allOf)) {
schema.allOf.forEach((subSchema: unknown) => {
if (subSchema && typeof subSchema === 'object') {
getAllowedProperties(subSchema as Record<string, unknown>, specification).forEach((prop) => properties.add(prop));
}
});
}
if (schema.oneOf && Array.isArray(schema.oneOf)) {
schema.oneOf.forEach((subSchema: unknown) => {
if (subSchema && typeof subSchema === 'object') {
getAllowedProperties(subSchema as Record<string, unknown>, specification).forEach((prop) => properties.add(prop));
}
});
}
return properties;
}
/**
* Validates a node against a schema.
* @param node The node to validate.
* @param schema The schema to validate against.
* @param path The path to the current node for error reporting.
* @param specification The root specification object.
*/
function validateNode(
node: unknown,
schema: Record<string, unknown> | null,
path: string,
specification: Record<string, unknown>
): string[] {
if (!schema || !node) return [];
if (schema.$ref && typeof schema.$ref === 'string') {
const ref = schema.$ref;
schema = getSchema(schema.$ref, specification) as Record<string, unknown> | null;
if (!schema) {
return [`Schema reference not found: ${ref} at ${path}`];
}
}
const errors: string[] = [];
if (schema.oneOf && Array.isArray(schema.oneOf)) {
const componentType = (node as Record<string, unknown>).type;
const sectionLayout = (node as Record<string, unknown>).layout;
let matchingSchema: unknown = null;
if (componentType && typeof componentType === 'string') {
const components = specification.components as Record<string, unknown>;
const componentTypeKey = Object.keys(components).find(
(key) => key.toLowerCase() === componentType.toLowerCase()
);
if (componentTypeKey) {
matchingSchema = schema.oneOf.find(
(s: Record<string, unknown>) => s.$ref === `#/components/${componentTypeKey}`
);
}
} else if (sectionLayout && typeof sectionLayout === 'string') {
const sections = specification.sections as Record<string, unknown>;
const sectionLayoutKey = Object.keys(sections).find(
(key) => key.toLowerCase() === sectionLayout.toLowerCase()
);
if (sectionLayoutKey) {
matchingSchema = schema.oneOf.find((s: Record<string, unknown>) => s.$ref === `#/sections/${sectionLayoutKey}`);
}
}
if (matchingSchema) {
return validateNode(node, matchingSchema as Record<string, unknown>, path, specification);
}
const oneOfErrors: string[][] = [];
for (const subSchema of schema.oneOf) {
const subErrors = validateNode(node, subSchema as Record<string, unknown>, path, specification);
if (subErrors.length === 0) {
return []; // Valid against one of the schemas
}
oneOfErrors.push(subErrors);
}
errors.push(`Invalid node at ${path}. It does not match any of the oneOf schemas.`);
return errors;
} else if (schema.allOf && Array.isArray(schema.allOf)) {
for (const subSchema of schema.allOf) {
errors.push(...validateNode(node, subSchema as Record<string, unknown>, path, specification));
}
} else {
const allowedProperties = getAllowedProperties(schema, specification);
if (node && typeof node === 'object' && !Array.isArray(node)) {
for (const key in node) {
if (key === 'component') continue;
if (!allowedProperties.has(key) && !schema.additionalProperties) {
errors.push(`Unknown property: ${path}.${key}`);
}
}
}
if (schema.properties && typeof schema.properties === 'object') {
for (const key in schema.properties) {
const prop = (schema.properties as Record<string, unknown>)[key] as { required?: boolean };
if (prop.required && (node as Record<string, unknown>)[key] === undefined) {
errors.push(`Missing required property: ${path}.${key}`);
}
}
}
for (const key in node as Record<string, unknown>) {
let propSchema: unknown = null;
if (schema.properties && typeof schema.properties === 'object' && (schema.properties as Record<string, unknown>)[key]) {
propSchema = (schema.properties as Record<string, unknown>)[key];
} else if (schema.additionalProperties) {
propSchema = schema.additionalProperties;
}
if (propSchema && typeof propSchema === 'object') {
const propPath = `${path}.${key}`;
const propValue = (node as Record<string, unknown>)[key];
const propSchemaRecord = propSchema as Record<string, unknown>;
if (propSchemaRecord.type) {
const types = Array.isArray(propSchemaRecord.type)
? (propSchemaRecord.type as string[])
: [propSchemaRecord.type as string];
let valid = false;
for (const type of types) {
if (type === 'integer') {
if (Number.isInteger(propValue)) {
valid = true;
break;
}
} else if (type === 'array') {
if (Array.isArray(propValue)) {
valid = true;
break;
}
} else {
if (typeof propValue === type) {
valid = true;
break;
}
}
}
if (!valid) {
errors.push(`Invalid type for ${propPath}. Expected ${types.join(' or ')}, got ${typeof propValue}`);
}
}
if (propSchemaRecord.enum && Array.isArray(propSchemaRecord.enum)) {
if (key === 'type' && typeof propValue === 'string') {
const lowerCaseEnum = propSchemaRecord.enum.map((v: unknown) => String(v).toLowerCase());
if (!lowerCaseEnum.includes(propValue.toLowerCase())) {
errors.push(
`Invalid value for ${propPath}. Expected one of ${propSchemaRecord.enum.join(', ')}, got ${propValue}`
);
}
} else if (!propSchemaRecord.enum.includes(propValue)) {
errors.push(
`Invalid value for ${propPath}. Expected one of ${propSchemaRecord.enum.join(', ')}, got ${propValue}`
);
}
}
if (propValue && typeof propValue === 'object') {
if (Array.isArray(propValue)) {
if (propSchemaRecord.items) {
propValue.forEach((item, index) => {
errors.push(
...validateNode(
item,
propSchemaRecord.items as Record<string, unknown>,
`${propPath}[${index}]`,
specification
)
);
});
}
} else {
errors.push(...validateNode(propValue, propSchemaRecord, propPath, specification));
}
}
}
}
}
return errors;
}
/**
* Validates a single content file.
* @param filePath The path to the content file.
* @param specification The merged specifications.
*/
async function validateFile(filePath: string, specification: Record<string, unknown>): Promise<string[]> {
const content = await fs.promises.readFile(filePath, 'utf-8');
const data = yaml.load(content);
const errors: string[] = [];
if (!data) return [];
const relativePath = path.relative(CWD, filePath);
if (relativePath.startsWith('content/pages')) {
const pageSchema = specification.Page as Record<string, unknown> | null;
if (!pageSchema) {
return [`Page specification not found.`];
}
errors.push(...validateNode(data, pageSchema, relativePath, specification));
} else if (relativePath.startsWith('content/shared')) {
const componentSchema = getSchema('#/definitions/EmbeddableComponent', specification) as
| Record<string, unknown>
| null;
if (!componentSchema) {
return [`EmbeddableComponent specification not found.`];
}
errors.push(...validateNode(data, componentSchema, relativePath, specification));
}
return errors;
}
/**
* The main function for the content validator script.
*/
async function main() {
const args = process.argv.slice(2);
const isDebug = args.includes('--debug');
const isQuiet = args.includes('--quiet');
if (!isQuiet) {
console.log('Starting content validation...');
}
const specification = (await loadSpecification()) as Record<string, unknown>;
const contentFiles = await loadContentFiles();
const allErrors: string[] = [];
if (isDebug) {
console.log('\nFound content files:');
contentFiles.forEach((file) => console.log(`- ${path.relative(CWD, file)}`));
console.log(`Total: ${contentFiles.length} files`);
}
for (const file of contentFiles) {
allErrors.push(...(await validateFile(file, specification)));
}
if (allErrors.length > 0) {
console.error('\nValidation failed with the following errors:');
allErrors.forEach((error) => console.error(`- ${error}`));
exit(1);
} else if (!isQuiet) {
console.log('\nAll content files are valid!');
}
if (!isQuiet) {
console.log('\nContent validation finished.');
}
}
main().catch((error) => {
console.error('Error during content validation:', error);
exit(1);
});