Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new script and ci for JSON Schema validation #452

Merged
merged 48 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
45435f9
feat: add schema validation script
AnimeshKumar923 Nov 10, 2023
713792d
fix: improve code, add more outputs
AnimeshKumar923 Nov 10, 2023
5471112
fix: improve code, remove comments
AnimeshKumar923 Nov 10, 2023
87c8218
fix: code change to previous version
AnimeshKumar923 Nov 13, 2023
420c7bc
fix: convert code from JS to TS
AnimeshKumar923 Nov 13, 2023
bdc3b6e
fix: code update and running on ts-node
AnimeshKumar923 Nov 13, 2023
297480d
fix: update code, add testing data
AnimeshKumar923 Nov 17, 2023
60436bc
arrange files, update code
AnimeshKumar923 Nov 21, 2023
f73101e
Revert "arrange files, update code"
AnimeshKumar923 Nov 21, 2023
d06a479
Revert "fix: update code, add testing data"
AnimeshKumar923 Nov 21, 2023
5d26cb2
arrange files, update code
AnimeshKumar923 Nov 21, 2023
e464858
update code, update config file
AnimeshKumar923 Nov 21, 2023
afa9db2
chore: re-organise file, update script
AnimeshKumar923 Nov 21, 2023
d584bb0
chore: rename files, update dependencies
AnimeshKumar923 Nov 21, 2023
196c6ab
add new script, update code
AnimeshKumar923 Nov 21, 2023
944a58a
update script
AnimeshKumar923 Nov 21, 2023
02412d2
update dependencies
AnimeshKumar923 Nov 21, 2023
9eadb79
remove test files
AnimeshKumar923 Nov 21, 2023
8da18f3
update workflow, update dependencies,update script
AnimeshKumar923 Nov 21, 2023
ba6fab7
Merge branch 'master' into json-schema-validation
smoya Nov 21, 2023
da96b36
move `ajv` dependencies to devDependencies
AnimeshKumar923 Nov 22, 2023
a71f753
Update code comment
AnimeshKumar923 Nov 22, 2023
8399eb9
remove `--if-present` tag
AnimeshKumar923 Nov 22, 2023
36940b8
chore: update code to filter files
AnimeshKumar923 Nov 25, 2023
2edcf9b
chore: re-add `2.0.0-rc1-without-$id.json`
AnimeshKumar923 Nov 25, 2023
eccb4ab
fix: update script and make more optimized
AnimeshKumar923 Nov 25, 2023
7f3f5bc
chore: remove comments, remove `.eslintrc.json`
AnimeshKumar923 Nov 25, 2023
b48c050
rename script in `package.json`
AnimeshKumar923 Nov 26, 2023
3570fe0
fix: update script, change instantiation of `ajv`
AnimeshKumar923 Nov 26, 2023
1efe30f
chore: update script in workflow
AnimeshKumar923 Nov 26, 2023
df35645
fix: update script, filter any non `.json` file
AnimeshKumar923 Nov 26, 2023
17fcffd
fix: update code, make script more optimized
AnimeshKumar923 Nov 26, 2023
b819893
Add `excludeFiles` to avoid failing script
AnimeshKumar923 Nov 28, 2023
26ffcd0
feat!: add 3.0.0 validation support (#462)
jonaslagoni Nov 30, 2023
f37fe59
chore(release): v6.0.0 (#463)
asyncapi-bot Nov 30, 2023
b50357c
Delete package-lock.json
AnimeshKumar923 Dec 1, 2023
4604fee
re-add package-lock.json
AnimeshKumar923 Dec 1, 2023
f30c583
Merge branch 'master' into json-schema-validation
AnimeshKumar923 Dec 1, 2023
c9f0675
Remove irrelevant code line
AnimeshKumar923 Dec 4, 2023
08045f1
Rename script and update the same in `package.json`
AnimeshKumar923 Dec 4, 2023
9584784
Update package-lock.json
AnimeshKumar923 Dec 4, 2023
75c0da2
Update `package-lock.json`
AnimeshKumar923 Dec 4, 2023
19103b6
feat: add mqtt bindings schemas (#464)
derberg Dec 4, 2023
dcde1ac
chore(release): v6.1.0 (#465)
asyncapi-bot Dec 4, 2023
1563f2c
Merge branch 'master' into json-schema-validation
smoya Dec 4, 2023
3cbe26d
Merge branch 'master' into json-schema-validation
smoya Dec 11, 2023
aeada06
fix: update script, update `package.json`
AnimeshKumar923 Dec 12, 2023
96f0178
Merge branch 'master' into json-schema-validation
derberg Dec 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/if-nodejs-pr-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,6 @@ jobs:
- if: steps.packagejson.outputs.exists == 'true'
name: Run release assets generation to make sure PR does not break it
run: npm run generate:assets --if-present
- if: steps.packagejson.outputs.exists == 'true'
name: Run JSON Schema Validator
run: npm run validate-schemas
24 changes: 23 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"bundle": "cd tools/bundler && npm i && npm run bundle",
"startNewVersion": "newVersion=$npm_config_new_version node scripts/add-new-version.js",
"lint": "echo 'No linter integrated yet'",
"bump:version": "npm --no-git-tag-version --allow-same-version version $VERSION"
"bump:version": "npm --no-git-tag-version --allow-same-version version $VERSION",
"validate-schemas": "node scripts/validate-schemas.js"
},
"repository": {
"type": "git",
Expand All @@ -37,7 +38,8 @@
},
"homepage": "https://github.com/asyncapi/spec-json-schemas#readme",
"devDependencies": {
"ajv": "^8.11.2",
"ajv": "^8.12.0",
"ajv-draft-04": "^1.0.0",
"mocha": "^10.0.0",
"nyc": "^15.1.0"
},
Expand Down
89 changes: 89 additions & 0 deletions scripts/validate-schemas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const fs = require('fs');
const path = require('path');

const AjvDraft04 = require('ajv-draft-04');
const ajvDraft04 = new AjvDraft04();

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

function validation (excludedFiles){

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

try{

const files = fs.readdirSync(directoryPath);

// Filter files
const filteredFiles = files.filter(file => !excludedFiles.includes(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(`\n${file}: JSON Schema is valid!`);
}
} else {
// Validate the schema
validate = ajv.validateSchema(obj);
if(validate){
console.log(`\n${file}: JSON Schema is valid!`);
}
}

// 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
});
}
} catch (error) {
validationErrors.push({
file,
errors: [{ message: `\nError reading or parsing JSON Schema: ${error.message}` }]
});
}
});

// Print errors after processing all files
validationErrors.forEach(({ file, errors }) => {
console.error(`\n${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('\nError during validation:', error.message);
}
}


const excludedFiles=['2.0.0-rc1.json', '2.0.0-rc1-without-$id.json']; // added temporarily to avoid validation failure due to these two files. The schemas version are incorrect in these and needs to be fixed.

validation(excludedFiles);

console.log('\nValidation completed successfully.');