import Ajv from 'ajv'; import fs from 'fs'; import path from 'path'; const ajv = new Ajv({verbose: true}); // Load and add the service schema const serviceSchema = JSON.parse(fs.readFileSync('./src/content/schema-services.json', 'utf-8')); ajv.addSchema(serviceSchema, 'serviceSchema'); // Load and add the category schema const categorySchema = JSON.parse(fs.readFileSync('./src/content/schema-categories.json', 'utf-8')); ajv.addSchema(categorySchema, 'categorySchema'); // Compile the category schema validator // const validate = ajv.getSchema('categorySchema'); /** * @param {string} filePath */ function validateFile(filePath) { const data = JSON.parse(fs.readFileSync(filePath, 'utf-8')); const validate = ajv.compile(categorySchema); const valid = validate(data); if (!valid) { throw new Error(`Invalid service data in ${filePath}: ${JSON.stringify(validate.errors)}`); } } /** * @param {string} directory */ function scanDirectory(directory) { const files = fs.readdirSync(directory); for (const file of files) { const filePath = path.join(directory, file); const stat = fs.statSync(filePath); if (stat.isDirectory()) { scanDirectory(filePath); } else if (path.extname(filePath) === '.json') { validateFile(filePath); } } } export function validateServices() { try { scanDirectory('./src/content/services'); console.log('All services validated successfully!'); } catch (error) { console.error(`Error validating services: ${error}`); console.log(ajv.errors); process.exit(1); // Exit with non-zero code to signal failure } } validateServices();