Skip to content

Commit

Permalink
add new script, update code
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AnimeshKumar923 committed Nov 21, 2023
1 parent d584bb0 commit 196c6ab
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 13 deletions.
104 changes: 104 additions & 0 deletions scripts/filter-file.js
Original file line number Diff line number Diff line change
@@ -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']);
14 changes: 1 addition & 13 deletions scripts/validate-schemas-final.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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 {
Expand All @@ -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);
}
});
}
Expand All @@ -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 => {
Expand All @@ -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 {
Expand All @@ -87,7 +76,6 @@ function draft07(){
}
} catch (error) {
console.error(`${file}: Error reading or parsing JSON Schema:`, error.message);
// draft04();
process.exit(1);
}
});
Expand Down

0 comments on commit 196c6ab

Please sign in to comment.