Files
3031023-xyz-public/scripts/validate.mjs
T

31 lines
1.6 KiB
JavaScript

import Ajv2020 from 'ajv/dist/2020.js';
import addFormats from 'ajv-formats';
import { readdir, readFile, stat } from 'node:fs/promises';
import { join } from 'node:path';
const readJson = async (path) => JSON.parse(await readFile(path, 'utf8'));
const ajv = new Ajv2020({ allErrors: true, strict: true });
addFormats(ajv);
const [intakeSchema, sitesSchema, siteSchema, validIntake, invalidIntake, sites] = await Promise.all([
readJson('schemas/intake.v1.schema.json'), readJson('schemas/sites.schema.json'), readJson('schemas/site.schema.json'),
readJson('fixtures/intake.v1.valid.json'), readJson('fixtures/intake.v1.invalid.json'), readJson('sites.json')
]);
const intakeCheck = ajv.compile(intakeSchema);
const sitesCheck = ajv.compile(sitesSchema);
const siteCheck = ajv.compile(siteSchema);
if (!intakeCheck(validIntake)) throw new Error(`Valid intake rejected: ${ajv.errorsText(intakeCheck.errors)}`);
if (intakeCheck(invalidIntake)) throw new Error('Invalid intake fixture was accepted');
if (!sitesCheck(sites)) throw new Error(`Sites manifest rejected: ${ajv.errorsText(sitesCheck.errors)}`);
for (const key of Object.keys(sites.sites)) {
const data = await readJson(`sites/${key}/site.json`);
if (!siteCheck(data)) throw new Error(`${key} site data rejected: ${ajv.errorsText(siteCheck.errors)}`);
}
for (const file of await readdir('public/images')) {
const info = await stat(join('public/images', file));
if (info.size > 500 * 1024) throw new Error(`Image exceeds the 500 KiB pilot limit: ${file}`);
}
console.log('Intake fixtures, deployment manifest, site data, and image limits are valid.');