Files
dynamic-sites-simple/scripts/validate-content.js
2026-04-17 16:08:31 -05:00

60 lines
1.7 KiB
JavaScript

#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const root = path.resolve(__dirname, '..');
// Dynamic import of shared schemas
const {
siteContextSchema,
sectionFileSchema,
eventsFileSchema,
smsSitesConfigSchema,
} = await import(path.join(root, 'shared/src/schemas/index.ts'));
const errors = [];
function validate(filePath, schema, label) {
const rel = path.relative(root, filePath);
try {
const raw = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
const result = schema.safeParse(raw);
if (!result.success) {
errors.push(`${rel}: ${result.error.issues.map(i => `${i.path.join('.')}: ${i.message}`).join('; ')}`);
} else {
console.log(`${rel}`);
}
} catch (e) {
errors.push(`${rel}: ${e.message}`);
}
}
console.log('Validating content...');
// site-context.json
validate(path.join(root, 'site-context.json'), siteContextSchema, 'site-context');
// content/events.json
validate(path.join(root, 'content/events.json'), eventsFileSchema, 'events');
// config/sms-sites.json
validate(path.join(root, 'config/sms-sites.json'), smsSitesConfigSchema, 'sms-sites');
// content/sections/*.json
const sectionsDir = path.join(root, 'content/sections');
if (fs.existsSync(sectionsDir)) {
for (const file of fs.readdirSync(sectionsDir).filter(f => f.endsWith('.json'))) {
validate(path.join(sectionsDir, file), sectionFileSchema, file);
}
}
if (errors.length > 0) {
console.error('\n✗ Validation failed:');
errors.forEach(e => console.error(` ${e}`));
process.exit(1);
} else {
console.log('\n✓ All content valid.');
}