From 196c6ab02325fefcb798dce7512bb3e5598d50aa Mon Sep 17 00:00:00 2001 From: Animesh Kumar Date: Tue, 21 Nov 2023 22:33:04 +0530 Subject: [PATCH] add new script, update code Changes: - created new `filter-file` script for filtering the files which has draft-04 and draft-07, and to process them separately - updated code, removed irrelevant comments --- scripts/filter-file.js | 104 ++++++++++++++++++++++++++++++ scripts/validate-schemas-final.js | 14 +--- 2 files changed, 105 insertions(+), 13 deletions(-) create mode 100644 scripts/filter-file.js diff --git a/scripts/filter-file.js b/scripts/filter-file.js new file mode 100644 index 00000000..abdc46b7 --- /dev/null +++ b/scripts/filter-file.js @@ -0,0 +1,104 @@ +const fs = require('fs'); +const path = require('path'); + + +function validation04(draft, startFileName, endFileName){ + + const Ajv = draft === 'draft04' ? require('ajv-draft-04') : require('ajv'); + const ajv = new Ajv(); + + // Specify the path to the 'schemas' directory + const directoryPath = '../schemas'; + + + const files = fs.readdirSync(directoryPath); + + // Filter files based on start and end file names + const filteredFiles = files.filter(file => { + return file >= startFileName && file <= endFileName; + }); + + + // 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); + + // Remove unnecessary definitions + delete obj.definitions['http://json-schema.org/draft-04/schema']; + delete obj.definitions['http://json-schema.org/draft-07/schema']; + + // Compile 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); + } + } catch (error) { + console.error(`${file}: Error reading or parsing JSON Schema:`, error.message); + process.exit(1); + } + }); +} + + +function validation(draft, startFileName, excludedFiles){ + + const Ajv = draft === 'draft04' ? require('ajv-draft-04') : require('ajv'); + const ajv = new Ajv(); + + // Specify the path to the 'schemas' directory + const directoryPath = '../schemas'; + + + const files = fs.readdirSync(directoryPath); + + // Filter files based on start and end file names + const filteredFiles = files.filter(file => { + return file >= startFileName && !excludedFiles.includes(file); + }); + + + // 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); + + // Remove unnecessary definitions + delete obj.definitions['http://json-schema.org/draft-04/schema']; + delete obj.definitions['http://json-schema.org/draft-07/schema']; + + // Compile 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); + } + } catch (error) { + console.error(`${file}: Error reading or parsing JSON Schema:`, error.message); + process.exit(1); + } + }); +} + + +validation04('draft04', '1.0.0-without-$id.json', '1.2.0.json'); +validation('draft07', '2.0.0-rc1.json', ['README.md', 'all.schema-store.json']); \ No newline at end of file diff --git a/scripts/validate-schemas-final.js b/scripts/validate-schemas-final.js index 630f38e6..df4885ca 100644 --- a/scripts/validate-schemas-final.js +++ b/scripts/validate-schemas-final.js @@ -10,8 +10,6 @@ function draft04(){ // Specify the path to the 'schemas' directory const directoryPath = '../schemas'; - // var json = globToRegExp("*.json"); - // Read the files from the 'schemas' directory fs.readdirSync(directoryPath).forEach(file => { @@ -30,9 +28,6 @@ function draft04(){ // Compile the schema const validate = ajv.validateSchema(obj); - // Check if the schema is valid - // const isSchemaValid = validate(schemaDocument); - if (validate) { console.log(`${file}: JSON Schema is valid!`); } else { @@ -41,8 +36,7 @@ function draft04(){ } } catch (error) { console.error(`${file}: Error reading or parsing JSON Schema:`, error.message); - draft07(); - // process.exit(1); + process.exit(1); } }); } @@ -56,8 +50,6 @@ function draft07(){ // Specify the path to the 'schemas' directory const directoryPath = '../schemas'; - // var json = globToRegExp("*.json"); - // Read the files from the 'schemas' directory fs.readdirSync(directoryPath).forEach(file => { @@ -76,9 +68,6 @@ function draft07(){ // Compile the schema const validate = ajv.validateSchema(obj); - // Check if the schema is valid - // const isSchemaValid = validate(schemaDocument); - if (validate) { console.log(`${file}: JSON Schema is valid!`); } else { @@ -87,7 +76,6 @@ function draft07(){ } } catch (error) { console.error(`${file}: Error reading or parsing JSON Schema:`, error.message); - // draft04(); process.exit(1); } });