From 00a132d7096aec9bf4685e009e150e6c37179a3c Mon Sep 17 00:00:00 2001 From: Mintu Gogoi <127925465+Min2who@users.noreply.github.com> Date: Sat, 13 Jan 2024 07:33:30 +0000 Subject: [PATCH 1/3] reduced the complexity --- scripts/validate-schemas.js | 110 +++++++++++++----------------------- 1 file changed, 40 insertions(+), 70 deletions(-) diff --git a/scripts/validate-schemas.js b/scripts/validate-schemas.js index 200f2c52..b7962ed1 100644 --- a/scripts/validate-schemas.js +++ b/scripts/validate-schemas.js @@ -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!`); + } else { + console.error(`\n${filePath}: JSON Schema is not valid:`, schemaValidator.errors); + } - // 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}` }] - }); - } - }); + 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 }); + } + }); - // 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']; validation(excludedFiles); - -console.log('\nValidation completed successfully.'); \ No newline at end of file +console.log('\nValidation completed successfully.'); From 082d019438430deddd9a958b51740d4e1067714d Mon Sep 17 00:00:00 2001 From: Mintu Date: Sat, 13 Jan 2024 16:17:36 +0530 Subject: [PATCH 2/3] reviewed changes --- scripts/validate-schemas.js | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/scripts/validate-schemas.js b/scripts/validate-schemas.js index b7962ed1..c065300f 100644 --- a/scripts/validate-schemas.js +++ b/scripts/validate-schemas.js @@ -9,17 +9,15 @@ 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); - if (validate) { - console.log(`\n${filePath}: JSON Schema is valid!`); - } else { - console.error(`\n${filePath}: JSON Schema is not valid:`, schemaValidator.errors); - } - - return validate; + return { filePath, validate, errors }; } catch (error) { - console.error(`\n${filePath}: Error reading or parsing JSON Schema: ${error.message}`); - return false; + return { + filePath, + validate: false, + errors: [{ message: `Error reading or parsing JSON Schema: ${error.message}` }], + }; } } @@ -29,7 +27,7 @@ function validation(excludedFiles) { try { const files = fs.readdirSync(directoryPath); const filteredFiles = files.filter(file => !excludedFiles.includes(file) && path.extname(file).toLowerCase() === '.json'); - + const validationErrors = []; filteredFiles.forEach(file => { @@ -39,14 +37,27 @@ function validation(excludedFiles) { return (obj.$schema === 'http://json-schema.org/draft-04/schema') ? ajvDraft04.validateSchema(obj) : ajv.validateSchema(obj); }; - if (!validateSchema(filePath, fs.readFileSync(filePath, 'utf8'), schemaValidator)) { - validationErrors.push({ file }); + 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(({ file }) => console.error(file)); + 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); + } + }); process.exit(1); } } catch (error) { From a4c108dfab62fbb29da2b2a1b2bdd9cd2092d35b Mon Sep 17 00:00:00 2001 From: Mintu Date: Sat, 20 Jan 2024 11:37:08 +0530 Subject: [PATCH 3/3] added comment --- scripts/validate-schemas.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/validate-schemas.js b/scripts/validate-schemas.js index c065300f..1922a451 100644 --- a/scripts/validate-schemas.js +++ b/scripts/validate-schemas.js @@ -66,6 +66,7 @@ function validation(excludedFiles) { } } -const excludedFiles = ['2.0.0-rc1.json', '2.0.0-rc1-without-$id.json']; +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.');