diff --git a/scripts/validate-schemas-final.js b/scripts/validate-schemas-final.js index efdd85e9..1b03c2d3 100644 --- a/scripts/validate-schemas-final.js +++ b/scripts/validate-schemas-final.js @@ -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); \ No newline at end of file +validation(); +console.log('Validation completed successfully.'); \ No newline at end of file