Skip to content

Commit

Permalink
fix: update code, make script more optimized
Browse files Browse the repository at this point in the history
Changes:

- made code to collect all errors in an array and print it after the
  remaining files have been validated

- suggestion applied from: asyncapi#452 (comment)
  • Loading branch information
AnimeshKumar923 committed Nov 26, 2023
1 parent df35645 commit 17fcffd
Showing 1 changed file with 65 additions and 43 deletions.
108 changes: 65 additions & 43 deletions scripts/validate-schemas-final.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,81 @@ const ajvDraft04 = new AjvDraft04();
const Ajv = require('ajv');
const ajv = new Ajv();

function validation (excludedFiles){
function validation (){

// Specify the path to the 'schemas' directory
const directoryPath = './schemas';


const files = fs.readdirSync(directoryPath);

// Filter files
const filteredFiles = files.filter(file => !excludedFiles.includes(file) && path.extname(file).toLowerCase() === '.json');


// Iterate through the filtered files
filteredFiles.forEach(file => {
// Construct the full path to the JSON schema file
const filePath = path.join(directoryPath, file);

try {
// Read and parse the JSON schema
const fileContent = fs.readFileSync(filePath, 'utf8');
const obj = JSON.parse(fileContent);

if(obj.$schema === 'http://json-schema.org/draft-04/schema'){

// Validate the schema
const validate = ajvDraft04.validateSchema(obj);

// Check if the schema is valid
if (validate) {
console.log(`${file}: JSON Schema is valid!`);
try{

const files = fs.readdirSync(directoryPath);

// Filter files
const filteredFiles = files.filter(file => path.extname(file).toLowerCase() === '.json');


// Collect errors in an array
const validationErrors = [];

// Iterate through the filtered files
filteredFiles.forEach(file => {
// Construct the full path to the JSON schema file
const filePath = path.join(directoryPath, file);


try {
// Read and parse the JSON schema
const fileContent = fs.readFileSync(filePath, 'utf8');
const obj = JSON.parse(fileContent);

let validate;
if (obj.$schema === 'http://json-schema.org/draft-04/schema') {
// Validate the schema
validate = ajvDraft04.validateSchema(obj);
if(validate){
console.log(`${file}: JSON Schema is valid!`);
}
} else {
console.error(`${file}: JSON Schema is not valid:`, ajv.errors);
process.exit(1);
// Validate the schema
validate = ajv.validateSchema(obj);
if(validate){
console.log(`${file}: JSON Schema is valid!`);
}
}
} else{
// Validate the schema
const validate = ajv.validateSchema(obj);

// Check if the schema is valid
if (validate) {
console.log(`${file}: JSON Schema is valid!`);
} else {
console.error(`${file}: JSON Schema is not valid:`, ajv.errors);
process.exit(1);

// Check if the schema is not valid and collect errors
if (!validate) {
validationErrors.push({
file,
errors: obj.$schema === 'http://json-schema.org/draft-04/schema'
? ajvDraft04.errors
: ajv.errors
});
// console.error(`${file}: JSON Schema is not valid:`, ajv.errors);
}
} catch (error) {
validationErrors.push({
file,
errors: [{ message: `Error reading or parsing JSON Schema: ${error.message}` }]
});
}
});

// Print errors after processing all files
validationErrors.forEach(({ file, errors }) => {
console.error(`${file}: JSON Schema is not valid:`, errors);
});

// Exit with an error code if there are validation errors
if (validationErrors.length > 0) {
process.exit(1);
}

} catch (error) {
console.error(`${file}: Error reading or parsing JSON Schema:`, error.message);
process.exit(1);
console.error('Error during validation:', error.message);
}
});
}

const excludedFiles=['2.0.0-rc1-without-$id.json', '2.0.0-rc1.json'];

validation(excludedFiles);
validation();
console.log('Validation completed successfully.');

0 comments on commit 17fcffd

Please sign in to comment.