Skip to content

Conversation

@n2h9
Copy link
Contributor

@n2h9 n2h9 commented Apr 26, 2025

Notes for Reviewers

This PR adds validation of document over json schema with golang lib.

This is just to show the case, not intended to be merged yet.

Signed commits

  • Yes, I signed my commits.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Hello @n2h9, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request introduces JSON schema validation for documents using a Go library. It includes updates to dependencies, adds new files for validation logic, and modifies existing JSON schema files to align with the new validation approach. The PR also incorporates error handling and testing for the validation process. As noted in the PR description, this is a proof-of-concept and not intended for immediate merging.

Highlights

  • JSON Schema Validation: Implements validation of documents against JSON schemas using a Go library (santhosh-tekuri/jsonschema).
  • Dependency Updates: Updates go.mod and go.sum to include the new JSON schema validation library and other dependencies like github.com/google/uuid and github.com/sirupsen/logrus.
  • Error Handling: Introduces a custom error type (ErrValidate) using meshkit/errors for better error reporting during validation.
  • Local Schema Loading: Adds functionality to load JSON schemas from local files, enabling validation against locally stored schema definitions.
  • Test Cases: Includes a test suite to validate both valid and invalid model files against the defined JSON schema.

Changelog

Click here to see the changelog
  • Makefile
    • Adds new error and error-util targets for analyzing and updating error codes using meshkit/cmd/errorutil.
  • go.mod
    • Updates Go version to 1.23.4.
    • Adds github.com/google/uuid, github.com/santhosh-tekuri/jsonschema, github.com/sirupsen/logrus, and github.com/stretchr/testify as dependencies.
    • Updates github.com/layer5io/meshkit to v0.8.26.
  • go.sum
    • Updates checksums for all dependencies, including newly added ones.
  • meshkiterrors/component_info.json
    • Creates a new file defining the component info for meshery.schemas, setting the type to client and the next error code to 2.
  • schemas/constructs/core.json
    • Removes the pattern property from the apiVersion schema definition.
  • schemas/constructs/v1alpha1/capability/capability.json
    • Updates the $id to https://schemas.meshery.io/v1alpha1/capability/capability.json.
  • schemas/constructs/v1beta1/category/category.json
    • Updates the $id to https://schemas.meshery.io/v1beta1/category/category.json.
  • schemas/constructs/v1beta1/connection.json
    • Updates the $id to https://schemas.meshery.io/v1beta1/connection.json.
  • schemas/constructs/v1beta1/model/model.json
    • Updates the $id to https://schemas.meshery.io/v1beta1/model/model.json.
    • Removes the $ref to ./openapi.yml#/components/schemas/Model from the model property.
  • schemas/constructs/v1beta1/subcategory/subcategory.json
    • Updates the $id to https://schemas.meshery.io/v1beta1/subcategory/subcategory.json.
  • validate/go/error.go
    • Creates a new file defining the ErrValidate error type using meshkitErrors.
  • validate/go/json_schema_loader.go
    • Creates a new file implementing a custom URL loader to load JSON schemas from local files, registering the https scheme to use this loader.
  • validate/go/logger.go
    • Creates a new file to set up a logger using meshkit/logger.
  • validate/go/testdata/model.aws-ec2-controller.v1.4.1.json
    • Creates a new file containing a valid model JSON for testing purposes.
  • validate/go/testdata/model.invalid.00.json
    • Creates a new file containing an invalid model JSON for testing purposes.
  • validate/go/validate.go
    • Creates a new file implementing the Validate and ValidateFromFilePath functions to validate documents against JSON schemas, including temporary directory setup and schema loading.
  • validate/go/validate_test.go
    • Creates a new file containing test cases for the ValidateFromFilePath function, testing both valid and invalid model files.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


A schema's embrace,
Validates data with grace,
Errors take their flight.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This PR introduces JSON schema validation using a Go library, which is a valuable addition for ensuring data integrity. The implementation involves creating a temporary directory to store schemas, setting up a URL loader, and using the jsonschema library for validation. Overall, the code is well-structured, but there are areas for improvement regarding error handling, TODOs, and schema ID updates.

Summary of Findings

  • Error Handling: The ErrValidate function uses meshkitErrors.Fatal which might be too severe for validation errors. Consider using a less severe error level like meshkitErrors.Error or meshkitErrors.Warn.
  • TODOs: The ErrValidate function contains TODO comments for error descriptions and remediation steps. These should be addressed to provide more informative error messages.
  • Schema ID Updates: The $id fields in the JSON schema files are being updated to include versioning. Ensure that all references to these schemas are also updated accordingly to maintain consistency.

Merge Readiness

The PR is not yet ready for merging. The TODO comments in the error handling need to be addressed, and the severity of the validation errors should be reviewed. Additionally, ensure that all schema references are updated to reflect the new versioned $id values. I am unable to approve this pull request, and recommend that it not be merged until the aforementioned issues are addressed.

func ErrValidate(errs ...error) error {
return meshkitErrors.New(
ErrValidateCode,
meshkitErrors.Fatal,
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Using meshkitErrors.Fatal might be too severe for validation errors. Consider using a less severe error level like meshkitErrors.Error or meshkitErrors.Warn depending on the context. This would allow the application to handle validation failures more gracefully.

Comment on lines +19 to +20
[]string{"TODO"},
[]string{"TODO"},
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

These TODO comments should be replaced with actual error descriptions and remediation steps to provide more informative error messages to the user. This will greatly improve the debuggability of the validation process.

,
		[]string{"Detailed error description here"},
		[]string{"Steps to resolve the error"},

@@ -1,5 +1,5 @@
{
"$id": "https://schemas.meshery.io/capability.json",
"$id": "https://schemas.meshery.io/v1alpha1/capability/capability.json",
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Updating the $id to include the version is good for schema management. Ensure that all references to this schema are updated accordingly in other schema files and in the code where this schema is used.

@@ -1,5 +1,5 @@
{
"$id": "https://schemas.meshery.io/category.json",
"$id": "https://schemas.meshery.io/v1beta1/category/category.json",
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Updating the $id to include the version is good for schema management. Ensure that all references to this schema are updated accordingly in other schema files and in the code where this schema is used.

@@ -1,5 +1,5 @@
{
"$id": "https://schemas.meshery.io/component.json",
"$id": "https://schemas.meshery.io/v1beta1/connection.json",
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Updating the $id to include the version is good for schema management. Ensure that all references to this schema are updated accordingly in other schema files and in the code where this schema is used.

@@ -1,5 +1,5 @@
{
"$id": "https://schemas.meshery.io/model.json",
"$id": "https://schemas.meshery.io/v1beta1/model/model.json",
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Updating the $id to include the version is good for schema management. Ensure that all references to this schema are updated accordingly in other schema files and in the code where this schema is used.

@@ -1,5 +1,5 @@
{
"$id": "https://schemas.meshery.io/category.json",
"$id": "https://schemas.meshery.io/v1beta1/subcategory/subcategory.json",
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Updating the $id to include the version is good for schema management. Ensure that all references to this schema are updated accordingly in other schema files and in the code where this schema is used.

"minLength": 2,
"maxLength": 100,
"description": "API version of the object",
"pattern": "([a-z.])*(?!^/)v(alpha|beta|[0-9]+)([.-]*[a-z0-9]+)*$",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(not about to be merged)

this keep failing with error of invalid reg exp, was not able to resolve, removed for now to be able to compile schema.

"description": "API version of the object",
"pattern": "([a-z.])*(?!^/)v(alpha|beta|[0-9]+)([.-]*[a-z0-9]+)*$",
"example": ["v1", "v1alpha1", "v2beta3", "v1.custom-suffix"]
"pattern": "^(([a-z.])+\/?)?v(alpha|beta|[0-9]+)([.-]*[a-z0-9]+)*$",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated version, which looks like very close to original, and is valid with respect to golang reg exp parser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant