Skip to content

Commit

Permalink
Merge branch 'master' into http_status_code
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Jan 30, 2024
2 parents 7214bfc + 992c509 commit 7702742
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 73 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@asyncapi/specs",
"version": "6.2.0",
"version": "6.2.1",
"description": "AsyncAPI schema versions",
"main": "index.js",
"types": "index.d.ts",
Expand Down
122 changes: 52 additions & 70 deletions scripts/validate-schemas.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,72 @@
const fs = require('fs');
const path = require('path');

const AjvDraft04 = require('ajv-draft-04');
const ajvDraft04 = new AjvDraft04();

const Ajv = require('ajv');
const ajv = new Ajv();

function validation (excludedFiles){
function validateSchema(filePath, fileContent, schemaValidator) {
try {
const obj = JSON.parse(fileContent);
const validate = schemaValidator(obj);
const errors = validate ? [] : (obj.$schema === 'http://json-schema.org/draft-04/schema' ? ajvDraft04.errors : ajv.errors);

return { filePath, validate, errors };
} catch (error) {
return {
filePath,
validate: false,
errors: [{ message: `Error reading or parsing JSON Schema: ${error.message}` }],
};
}
}

// Specify the path to the 'schemas' directory
function validation(excludedFiles) {
const directoryPath = './schemas';

try{

try {
const files = fs.readdirSync(directoryPath);
const filteredFiles = files.filter(file => !excludedFiles.includes(file) && path.extname(file).toLowerCase() === '.json');

// Filter files
const filteredFiles = files.filter(file => !excludedFiles.includes(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(`\n${file}: JSON Schema is valid!`);
}
} else {
// Validate the schema
validate = ajv.validateSchema(obj);
if(validate){
console.log(`\n${file}: JSON Schema is valid!`);
}
}

// 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
});
}
} catch (error) {
validationErrors.push({
file,
errors: [{ message: `\nError reading or parsing JSON Schema: ${error.message}` }]
});
}
});

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

// Exit with an error code if there are validation errors
if (validationErrors.length > 0) {
process.exit(1);
filteredFiles.forEach(file => {
const filePath = path.join(directoryPath, file);

const schemaValidator = (obj) => {
return (obj.$schema === 'http://json-schema.org/draft-04/schema') ? ajvDraft04.validateSchema(obj) : ajv.validateSchema(obj);
};

const validationResult = validateSchema(filePath, fs.readFileSync(filePath, 'utf8'), schemaValidator);

if (!validationResult.validate || validationResult.errors.length > 0) {
validationErrors.push(validationResult);
}
});

if (validationErrors.length > 0) {
console.error('\nValidation errors:');
validationErrors.forEach(({ filePath, validate, errors }) => {
console.error(`${filePath}: JSON Schema is not valid:`);

if (validate) {
console.error('Detailed Error Information:');
errors.forEach(error => {
console.error(JSON.stringify(error, null, 2));
});
} else {
console.error(errors);
}

} catch (error) {
console.error('\nError during validation:', error.message);
process.exit(1);
});
process.exit(1);
}
} catch (error) {
console.error('\nError during validation:', error.message);
process.exit(1);
}
}


const excludedFiles=['2.0.0-rc1.json', '2.0.0-rc1-without-$id.json']; // added temporarily to avoid validation failure due to these two files. The schemas version are incorrect in these and needs to be fixed.
const excludedFiles = ['2.0.0-rc1.json', '2.0.0-rc1-without-$id.json']; // added temporarily to avoid validation failure due to these two files. The schemas version are incorrect in these and needs to be fixed.

validation(excludedFiles);

console.log('\nValidation completed successfully.');
console.log('\nValidation completed successfully.');

0 comments on commit 7702742

Please sign in to comment.