Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reduced the complexity of validate-schema.js #483

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 40 additions & 70 deletions scripts/validate-schemas.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,60 @@
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){

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

try{
function validateSchema(filePath, fileContent, schemaValidator) {
try {
const obj = JSON.parse(fileContent);
const validate = schemaValidator(obj);

const files = fs.readdirSync(directoryPath);
if (validate) {
console.log(`\n${filePath}: JSON Schema is valid!`);
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
} else {
console.error(`\n${filePath}: JSON Schema is not valid:`, schemaValidator.errors);
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
}

// Filter files
const filteredFiles = files.filter(file => !excludedFiles.includes(file) && path.extname(file).toLowerCase() === '.json');
return validate;
} catch (error) {
console.error(`\n${filePath}: Error reading or parsing JSON Schema: ${error.message}`);
return false;
}
}

function validation(excludedFiles) {
const directoryPath = './schemas';

// Collect errors in an array
const validationErrors = [];
try {
const files = fs.readdirSync(directoryPath);
const filteredFiles = files.filter(file => !excludedFiles.includes(file) && path.extname(file).toLowerCase() === '.json');

// Iterate through the filtered files
const validationErrors = [];

filteredFiles.forEach(file => {
// Construct the full path to the JSON schema file
const filePath = path.join(directoryPath, 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}` }]
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
});
}
});
const schemaValidator = (obj) => {
return (obj.$schema === 'http://json-schema.org/draft-04/schema') ? ajvDraft04.validateSchema(obj) : ajv.validateSchema(obj);
};

// Print errors after processing all files
validationErrors.forEach(({ file, errors }) => {
console.error(`\n${file}: JSON Schema is not valid:`, errors);
});
if (!validateSchema(filePath, fs.readFileSync(filePath, 'utf8'), schemaValidator)) {
validationErrors.push({ file });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are skipping a very important part: the collection of the errors. It was here #483 (comment)

}
});

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

} catch (error) {
console.error('\nError during validation:', error.message);
process.exit(1);
if (validationErrors.length > 0) {
console.error('\nValidation errors:');
validationErrors.forEach(({ file }) => console.error(file));
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'];
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
validation(excludedFiles);

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