Skip to content

Commit

Permalink
update script
Browse files Browse the repository at this point in the history
Changes:

- updated `validate-schemas-final.js` to match the `filter-file.js`
- `filter-file.js` contains the most accurate code as of now
  • Loading branch information
AnimeshKumar923 committed Nov 21, 2023
1 parent 196c6ab commit 944a58a
Showing 1 changed file with 69 additions and 52 deletions.
121 changes: 69 additions & 52 deletions scripts/validate-schemas-final.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,103 @@ const fs = require('fs');
const path = require('path');


function draft04(){
const Ajv = require("ajv-draft-04");
const ajv = new Ajv();
function validationDraft04(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';

// Read the files from the 'schemas' directory

fs.readdirSync(directoryPath).forEach(file => {
// Construct the full path to the JSON schema file
const filePath = path.join(directoryPath, file);
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 {
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!`);
console.log(`${file}: JSON Schema is valid!`);
} else {
console.error(`${file}: JSON Schema is not valid:`, ajv.errors);
process.exit(1);
console.error(`${file}: JSON Schema is not valid:`, ajv.errors);
process.exit(1);
}
} catch (error) {
} catch (error) {
console.error(`${file}: Error reading or parsing JSON Schema:`, error.message);
process.exit(1);
}
});
}
});
}



function draft07(){
const Ajv = require("ajv");
const ajv = new Ajv();

// Specify the path to the 'schemas' directory
const directoryPath = '../schemas';

// Read the files from the 'schemas' directory

fs.readdirSync(directoryPath).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);

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

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

}


draft04();
draft07();
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']);

0 comments on commit 944a58a

Please sign in to comment.