46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
const Ajv = require('ajv');
|
|
const addFormats = require('ajv-formats');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const schema = require('./token-schema.json');
|
|
|
|
function validateJsonFile(filePath) {
|
|
try {
|
|
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
const valid = validate(data);
|
|
if (valid) {
|
|
console.log(`File ${path.basename(filePath)} is valid.`);
|
|
} else {
|
|
console.log(`File ${path.basename(filePath)} is invalid:`, validate.errors);
|
|
}
|
|
} catch (error) {
|
|
console.error(`Error reading or parsing the file ${path.basename(filePath)}:`, error);
|
|
}
|
|
}
|
|
|
|
function validateAllJsonFilesInDirectory(directoryPath) {
|
|
fs.readdir(directoryPath, (err, files) => {
|
|
if (err) {
|
|
console.error('Error reading directory:', err);
|
|
return;
|
|
}
|
|
|
|
files.forEach(file => {
|
|
const filePath = path.join(directoryPath, file);
|
|
if (path.extname(file) === '.json') {
|
|
validateJsonFile(filePath);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
const ajv = new Ajv();
|
|
addFormats(ajv);
|
|
|
|
const validate = ajv.compile(schema);
|
|
|
|
const dataDirectoryPath = path.resolve(process.cwd(), 'data');
|
|
|
|
validateAllJsonFilesInDirectory(dataDirectoryPath);
|