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

Conversation

AnimeshKumar923
Copy link
Contributor

@AnimeshKumar923 AnimeshKumar923 commented Nov 21, 2023

Description

Tasks

  • Create a JSON schema validation script. (this is the script)
  • Integrate JavaScript linter. (ESLint?) Change the existing if-nodejs-pr-testing.yml to lint JavaScript files. (Will be converted into a new issue in the future. UPDATE: the issue has been raised Add code linting (eslint) as part of CI script #467 )
  • Change the existing if-nodejs-pr-testing.yml to validate incoming schemas. (implemented here) Instead, do changes in test field of the script and include execution of the script from there itself (implemented here)

Related issue(s)
Fixes #436

Changes:

- added schema validation script
- update package.json to match current dependencies
Changes:

- Changed code to previous version for a re-attempt for building the
  solution.
- Will start from a fresh approach.
Changes:

- code updated to TS and is giving the desired output when run through
  ts-node.
- output is: `error: reference "http://json-schema.org/draft-07/schema"
  resolves to more than one schema`
- tsconfig.json is added because it's required when we run ts-node and
  execute the script.
-ts-node can be installed in the CI at the runtime.
Changes:

- modify code with help of Sergio Moya (thank you!)
- added test cases of Schema by Sergio
- convert from TypeScript to JavaScript
Changes:

- categorize file into draft-04 and draft-07 for ease of
  differentiation.
- Code updated to run successfully for the draft-07 files.
Changes:

- categorize file into draft-04 and draft-07 for ease of
  differentiation.
- code updated to run successfully for the draft-07 files.
- update package.json
Changes:

- updated the dependencies
- rename config.json file
- separete the validate-schema file into two versions
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
Changes:

- updated `validate-schemas-final.js` to match the `filter-file.js`
- `filter-file.js` contains the most accurate code as of now
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome to AsyncAPI. Thanks a lot for creating your first pull request. Please check out our contributors guide useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

Changes:

- update workflow to run JSON Schema validator
- update `package.json` to include the validator
- update directory path to run script from root of the project
@AnimeshKumar923
Copy link
Contributor Author

AnimeshKumar923 commented Nov 21, 2023

@smoya

  • I've made the script a bit more exclusive for the draft-04 files. Made two functions, one for the draft-04 files and other for draft-07 ones assuming that in future, incoming file changes will be using draft-07 or more latest ones.
  • Updated the workflow to include lint and lint-schemas steps.
  • Update package.json dependencies and scripts section to include lint and lint-schemas commands.

I'm using ESLint for the linting of all the JavaScript files. I've seen this being implemented in the website repository. Is it okay here?

Please have a look and provide your feedback on this. Thank you. 🙏

Copy link
Member

@smoya smoya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm so happy to see how close we are of having validation of our JSON Schema files in CI!

You did a really great job here 👏 👏 .

I wrote some comments, suggestions, and other things that I believe we must fix. Feel free to ask any question.

package.json Outdated Show resolved Hide resolved
scripts/validate-schemas-final.js Outdated Show resolved Hide resolved
scripts/validate-schemas-final.js Outdated Show resolved Hide resolved
scripts/validate-schemas-final.js Outdated Show resolved Hide resolved
scripts/validate-schemas-final.js Outdated Show resolved Hide resolved
package-lock.json Show resolved Hide resolved
.eslintrc.json Outdated Show resolved Hide resolved
.github/workflows/if-nodejs-pr-testing.yml Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
smoya
smoya previously approved these changes Dec 11, 2023
@smoya
Copy link
Member

smoya commented Dec 11, 2023

@AnimeshKumar923 there is one code smell SonarCloud complains https://sonarcloud.io/project/issues?resolved=false&types=CODE_SMELL&pullRequest=452&id=asyncapi_asyncapi-node&open=AYwW7h_-2LUoXdDhnTTh.

It is about simplifying the code basically. I agree, it can be simplified and improved and I invite you to do it, but I would say this can be done in a separate PR. To be honest, I find this code to be readable and simple enough at this stage IMHO. I would say you can create a follow up issue in order to simplify that.

Regarding linting, and as we do not have a linter in place, I won't required you to pass any linting. Instead, we will fix those once #467 is done.

@smoya
Copy link
Member

smoya commented Dec 11, 2023

@derberg ping 🙌

Copy link
Member

@derberg derberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you cannot modify .github/workflows/if-nodejs-pr-testing.yml as it comes from .github and whatever you modify now can be overwritten later

better just modify test script in package.json and make it fun the validate-schemas script, so instead of "test": "npm run build && nyc mocha", do "test": "npm run build && nyc mocha && npm run validate-schemas",. This way you do not have to modify .github/workflows/if-nodejs-pr-testing.yml

last but not least, please do not introduce scripts names with - as we follow approach with :. So instead of validate-schemas better make validate:schemas


also did you check if you have proper error handling in script that will fail GitHub action in case of validation fails? so we know that it actually gets red when error happens? I see you have one try/catch that is just doing console.error and it will for sure not terminate script exacution

@AnimeshKumar923
Copy link
Contributor Author

@AnimeshKumar923 there is one code smell SonarCloud complains https://sonarcloud.io/project/issues?resolved=false&types=CODE_SMELL&pullRequest=452&id=asyncapi_asyncapi-node&open=AYwW7h_-2LUoXdDhnTTh.

It is about simplifying the code basically. I agree, it can be simplified and improved and I invite you to do it, but I would say this can be done in a separate PR.

It's a new thing for me (the code smell part), still learning from it.

To be honest, I find this code to be readable and simple enough at this stage IMHO.

Okay, I see

I would say you can create a follow up issue in order to simplify that.

Yes, I agree. For the code optimization, I would like to do it in a new issue and it might take some time as per my current knowledge about JavaScript and programming in general. 😅

Regarding linting, and as we do not have a linter in place, I won't required you to pass any linting. Instead, we will fix those once #467 is done.

Understood.

@AnimeshKumar923
Copy link
Contributor Author

Hi @derberg! 👋

Thank you for your detailed review. I'll definitely look into the things you mentioned. 👍

Changes:

- Revert 'if-nodejs-pr-testing.yml' to original condition.

- Update 'package.json', changed "test" field to include the JSON Schema validation.

- Change naming convention, from 'validate-schema' to 'validate:schema'.

- Modify error handling 'catch' block on line 79 to terminate script if
  the schema is not valid.

- Applied suggestion from: asyncapi#452 (review)
@AnimeshKumar923
Copy link
Contributor Author

Hey @smoya @derberg 👋

Please have a look at the files now. I've made changes suggested here through aeada06

@derberg
Copy link
Member

derberg commented Dec 12, 2023

lgtm, I see also that validation runs: https://github.com/asyncapi/spec-json-schemas/actions/runs/7180191694/job/19557877973?pr=452

did you at some point of time try to simulate an error to see if test will fail?

@AnimeshKumar923
Copy link
Contributor Author

lgtm, I see also that validation runs: https://github.com/asyncapi/spec-json-schemas/actions/runs/7180191694/job/19557877973?pr=452

did you at some point of time try to simulate an error to see if test will fail?

@derberg Yes. I created a dummy 0.0.0.json and tested it.

Contents of 0.0.0.json 👇 (thanks goes to Sergio for providing this template for testing!)

{
"$id":"http://foo.bar",
"$schema":"http://json-schema.org/draft-07/schema",
    "definitions":{
        "http://json-schema.org/draft-07/schema":{},
        "person":{
            "type":"object",
            "properties":{
                "name":{
                    "type":"invalid"
                }
            }
        }
    }
}

These are the results after running node scripts/validate-schemas.js 👇

1.0.0-without-$id.json: JSON Schema is valid!

1.0.0.json: JSON Schema is valid!

1.1.0-without-$id.json: JSON Schema is valid!

1.1.0.json: JSON Schema is valid!

1.2.0-without-$id.json: JSON Schema is valid!

1.2.0.json: JSON Schema is valid!

2.0.0-rc2-without-$id.json: JSON Schema is valid!

2.0.0-rc2.json: JSON Schema is valid!

2.0.0-without-$id.json: JSON Schema is valid!

2.0.0.json: JSON Schema is valid!

2.1.0-without-$id.json: JSON Schema is valid!

2.1.0.json: JSON Schema is valid!

2.2.0-without-$id.json: JSON Schema is valid!

2.2.0.json: JSON Schema is valid!

2.3.0-without-$id.json: JSON Schema is valid!

2.3.0.json: JSON Schema is valid!

2.4.0-without-$id.json: JSON Schema is valid!

2.4.0.json: JSON Schema is valid!

2.5.0-without-$id.json: JSON Schema is valid!

2.5.0.json: JSON Schema is valid!

2.6.0-without-$id.json: JSON Schema is valid!

2.6.0.json: JSON Schema is valid!

3.0.0-without-$id.json: JSON Schema is valid!

3.0.0.json: JSON Schema is valid!

all.schema-store.json: JSON Schema is valid!

0.0.0.json: JSON Schema is not valid: [
  {
    instancePath: '/definitions/person/properties/name/type',
    schemaPath: '#/definitions/simpleTypes/enum',
    keyword: 'enum',
    params: { allowedValues: [Array] },
    message: 'must be equal to one of the allowed values'
  },
  {
    instancePath: '/definitions/person/properties/name/type',
    schemaPath: '#/properties/type/anyOf/1/type',
    keyword: 'type',
    params: { type: 'array' },
    message: 'must be array'
  },
  {
    instancePath: '/definitions/person/properties/name/type',
    schemaPath: '#/properties/type/anyOf',
    keyword: 'anyOf',
    params: {},
    message: 'must match a schema in anyOf'
  }
]

and when executed echo $?, it resulted in 1.

Do we have any ways of testing this dummy in the PR itself for better assurance? 👀

Copy link

sonarcloud bot commented Dec 13, 2023

Quality Gate Passed Quality Gate passed

The SonarCloud Quality Gate passed, but some issues were introduced.

1 New issue
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link
Member

@derberg derberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnimeshKumar923 this PR is already running your test script 😄

@smoya LGTM so if it comes to me, we can merge

@smoya
Copy link
Member

smoya commented Dec 13, 2023

/rtm

@asyncapi-bot asyncapi-bot merged commit 3768128 into asyncapi:master Dec 13, 2023
10 checks passed
@AnimeshKumar923
Copy link
Contributor Author

@smoya Thank you so much for your guidance throughout this PR! I'm very happy how it turned out in the end. 🙇 🚀 🥳
Also, let me see what can I do about the code smell in a new issue 😅

@derberg Thank you for your in-depth review. It was very crucial for this PR. 🙇 👍

Looking forward to do more contributions here.

Cheers! 🥂

@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 6.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@aeworxet
Copy link

@asyncapi/bounty_team

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bounty AsyncAPI Bounty program related label released
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

[CI] Lint and validate JSON Schema docs
7 participants