KkosmetickySalon/tests/ValidateServices.js

42 lines
1.2 KiB
JavaScript
Raw Normal View History

import Ajv from 'ajv';
import fs from 'fs';
import path from 'path';
const serviceSchema = JSON.parse(fs.readFileSync('./src/content/schema-categories.json', 'utf-8'));
function validateFile(filePath) {
const ajv = new Ajv();
const data = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
const validate = ajv.compile(serviceSchema);
const valid = validate(data);
if (!valid) {
throw new Error(`Invalid service data in ${filePath}: ${JSON.stringify(validate.errors)}`);
}
}
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/');
console.log('All services validated successfully!');
} catch (error) {
console.error(`Error validating services: ${error}`);
process.exit(1); // Exit with non-zero code to signal failure
}
}
validateServices();