Skip to content

Commit

Permalink
chore: update code to filter files
Browse files Browse the repository at this point in the history
Changes:

- update code to filter files and exclude files which are not to be
  validated.
- will be modified later to be more optimized according to this:
  #452 (comment)
  • Loading branch information
AnimeshKumar923 committed Nov 25, 2023
1 parent 8399eb9 commit 36940b8
Showing 1 changed file with 54 additions and 47 deletions.
101 changes: 54 additions & 47 deletions scripts/validate-schemas-final.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
I agree @char0n's comment. You can iterate over all schemas in /schemas and determine their metaschema (draft4 or draft7). You can check the $schema field once you parse the file and you will get either http://json-schema.org/draft-04/schema or http://json-schema.org/draft-07/schema. Then you instantiate one or another ajv version. Does it sound reasonable?
*/

const fs = require('fs');
const path = require('path');

Expand Down Expand Up @@ -51,54 +57,55 @@ function validationDraft04(draft, startFileName, endFileName){
}


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'];

// 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);
}
} catch (error) {
console.error(`${file}: Error reading or parsing JSON Schema:`, error.message);
process.exit(1);
}
});
function validation (excludedFiles){

const Ajv = require('ajv');
const ajv = new Ajv();

// 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));


// 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'];

// 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);
}
} catch (error) {
console.error(`${file}: Error reading or parsing JSON Schema:`, error.message);
process.exit(1);
}
});
}


const excludedFiles=['README.md', 'all.schema-store.json', '1.0.0-without-$id.json', '1.0.0.json', '1.1.0-without-$id.json','1.1.0.json', '1.2.0-without-$id.json', '1.2.0.json', '2.0.0-rc1-without-$id.json'];

validationDraft04('draft04', '1.0.0-without-$id.json', '1.2.0.json');
validation('draft07', '2.0.0-rc1.json', ['README.md', 'all.schema-store.json']);

validation(excludedFiles);

0 comments on commit 36940b8

Please sign in to comment.