-
Notifications
You must be signed in to change notification settings - Fork 75
feature: validate document with go lib over json schema #306
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "name": "meshery.schemas", | ||
| "type": "client", | ||
| "next_error_code": 2 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,8 +13,8 @@ | |
| "minLength": 2, | ||
| "maxLength": 100, | ||
| "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]+)*$", | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| "example": ["v1", "v1alpha1", "v2beta3", "v1.custom-suffix", "models.meshery.io/v1beta1"] | ||
| }, | ||
| "semverString": { | ||
| "type": "string", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "$id": "https://schemas.meshery.io/capability.json", | ||
| "$id": "https://schemas.meshery.io/v1alpha1/capability/capability.json", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "description": "Meshery manages entities in accordance with their specific capabilities. This field explicitly identifies those capabilities largely by what actions a given component supports; e.g. metric-scrape, sub-interface, and so on. This field is extensible. Entities may define a broad array of capabilities, which are in-turn dynamically interpretted by Meshery for full lifecycle management.", | ||
| "additionalProperties": false, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "$id": "https://schemas.meshery.io/category.json", | ||
| "$id": "https://schemas.meshery.io/v1beta1/category/category.json", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "type": "object", | ||
| "description": "Category of the model.", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "$id": "https://schemas.meshery.io/component.json", | ||
| "$id": "https://schemas.meshery.io/v1beta1/connection.json", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "description": "Meshery Connections are managed and unmanaged resources that either through discovery or manual entry are tracked by Meshery. Learn more at https://docs.meshery.io/concepts/logical/connections", | ||
| "additionalProperties": false, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "$id": "https://schemas.meshery.io/model.json", | ||
| "$id": "https://schemas.meshery.io/v1beta1/model/model.json", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "description": "Meshery Models serve as a portable unit of packaging to define managed entities, their relationships, and capabilities.", | ||
| "additionalProperties": false, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "$id": "https://schemas.meshery.io/category.json", | ||
| "$id": "https://schemas.meshery.io/v1beta1/subcategory/subcategory.json", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "type": "string", | ||
| "title": "SubCategory", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package validate | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
| meshkitErrors "github.com/layer5io/meshkit/errors" | ||
| ) | ||
|
|
||
| const ( | ||
| ErrValidateCode = "meshery.schemas-1" | ||
| ) | ||
|
|
||
| func ErrValidate(errs ...error) error { | ||
| return meshkitErrors.New( | ||
| ErrValidateCode, | ||
| meshkitErrors.Fatal, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| []string{"Error validating document"}, | ||
| []string{errors.Join(errs...).Error()}, | ||
| []string{"TODO"}, | ||
| []string{"TODO"}, | ||
|
Comment on lines
+19
to
+20
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These ,
[]string{"Detailed error description here"},
[]string{"Steps to resolve the error"}, |
||
| ) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package validate | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/google/uuid" | ||
| jsFormats "github.com/santhosh-tekuri/jsonschema/formats" | ||
| jsLoader "github.com/santhosh-tekuri/jsonschema/loader" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| const schemaURL = "https://schemas.meshery.io" | ||
|
|
||
| type urlLoader struct { | ||
| baseFolder string | ||
| } | ||
|
|
||
| var ul = &urlLoader{} | ||
|
|
||
| func setUpBaseFolder(folder string) { | ||
| ul.baseFolder = folder | ||
| } | ||
|
|
||
| func init() { | ||
| // uuid is not a custom format | ||
| jsFormats.Register("uuid", func(v string) bool { | ||
| if _, err := uuid.Parse(v); err != nil { | ||
| log.Warnf("invalid uuid format %s %v", v, err) | ||
| return false | ||
| } | ||
| return true | ||
| }) | ||
|
|
||
| // to load from local | ||
| jsLoader.Register( | ||
| "https", | ||
| ul, | ||
| ) | ||
| } | ||
|
|
||
| func (ul urlLoader) Load(path string) (io.ReadCloser, error) { | ||
| localPath := path | ||
| if strings.HasPrefix(path, schemaURL) { | ||
| localPath = filepath.Join( | ||
| ul.baseFolder, | ||
| strings.TrimLeft( | ||
| strings.TrimPrefix(path, schemaURL), | ||
| "/", | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| if strings.HasSuffix(localPath, ".yaml") || strings.HasSuffix(localPath, ".yml") { | ||
| // return io.NopCloser(bytes.NewReader([]byte("{}"))), nil | ||
| return loadYAMLFile(localPath) | ||
| } | ||
|
|
||
| return os.Open(localPath) | ||
| } | ||
|
|
||
| // loadYAMLFile loads a YAML file, converts it to JSON, and returns it as io.ReadCloser | ||
| func loadYAMLFile(path string) (io.ReadCloser, error) { | ||
| // Read the YAML file | ||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Parse YAML into an arbitrary Go structure | ||
| var yamlData interface{} | ||
| if err := yaml.Unmarshal(data, &yamlData); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Marshal it back into JSON | ||
| jsonData, err := json.Marshal(yamlData) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Return a ReadCloser over the JSON data | ||
| return io.NopCloser(bytes.NewReader(jsonData)), nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package validate | ||
|
|
||
| import ( | ||
| "github.com/layer5io/meshkit/logger" | ||
| "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| var log logger.Handler | ||
|
|
||
| func SetLogger(l logger.Handler) { | ||
| log = l | ||
| } | ||
|
|
||
| func initLoggerOnce() error { | ||
| if log != nil { | ||
| return nil | ||
| } | ||
|
|
||
| var err error | ||
| log, err = logger.New( | ||
| "schemas", | ||
| logger.Options{ | ||
| Format: logger.SyslogLogFormat, | ||
| LogLevel: int(logrus.InfoLevel), | ||
| }, | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| { | ||
| "id": "00000000-0000-0000-0000-000000000000", | ||
| "schemaVersion": "models.meshery.io/v1beta1", | ||
| "version": "v1.0.0", | ||
| "name": "aws-ec2-controller", | ||
| "displayName": "AWS EC2", | ||
| "status": "enabled", | ||
| "description": "---- this field was not here ----", | ||
| "relationshipsCount": 0, | ||
| "componentsCount": 0, | ||
| "categoryId": "00000000-0000-0000-0000-000000000000", | ||
| "registrantId": "00000000-0000-0000-0000-000000000000", | ||
| "registrant": { | ||
| "id": "00000000-0000-0000-0000-000000000000", | ||
| "name": "Github", | ||
| "credential_id": "00000000-0000-0000-0000-000000000000", | ||
| "type": "registry", | ||
| "sub_type": "", | ||
| "kind": "github", | ||
| "status": "discovered", | ||
| "user_id": "00000000-0000-0000-0000-000000000000", | ||
| "created_at": "0001-01-01T00:00:00Z", | ||
| "updated_at": "0001-01-01T00:00:00Z", | ||
| "deleted_at": "0001-01-01T00:00:00Z" | ||
| }, | ||
| "category": { | ||
| "id": "00000000-0000-0000-0000-000000000000", | ||
| "name": "Provisioning", | ||
| "metadata": {} | ||
| }, | ||
| "subCategory": "Compute", | ||
| "metadata": { | ||
| "capabilities": [], | ||
| "isAnnotation": false, | ||
| "primaryColor": "#ED7100", | ||
| "shape": "rectangle", | ||
| "styleOverrides": "", | ||
| "svgColor": "\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\u003c!DOCTYPE svg\u003e\u003csvg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 40 40\" height=\"20\" width=\"20\"\u003e \u003cg xmlns=\"http://www.w3.org/2000/svg\" id=\"Icon-Architecture/32/Arch_Amazon-EC2_32\" stroke=\"none\" stroke-width=\"1\" fill=\"none\" fill-rule=\"evenodd\"\u003e \u003cg xmlns=\"http://www.w3.org/2000/svg\" id=\"Icon-Architecture-BG/32/Compute\" fill=\"#ED7100\"\u003e \u003crect xmlns=\"http://www.w3.org/2000/svg\" id=\"Rectangle\" x=\"0\" y=\"0\" width=\"40\" height=\"40\"\u003e\u003c/rect\u003e \u003c/g\u003e \u003cpath xmlns=\"http://www.w3.org/2000/svg\" d=\"M26.052,27 L26,13.948 L13,14 L13,27.052 L26.052,27 Z M27,14 L29,14 L29,15 L27,15 L27,17 L29,17 L29,18 L27,18 L27,20 L29,20 L29,21 L27,21 L27,23 L29,23 L29,24 L27,24 L27,26 L29,26 L29,27 L27,27 L27,27.052 C27,27.575 26.574,28 26.052,28 L26,28 L26,30 L25,30 L25,28 L23,28 L23,30 L22,30 L22,28 L20,28 L20,30 L19,30 L19,28 L17,28 L17,30 L16,30 L16,28 L14,28 L14,30 L13,30 L13,28 L12.948,28 C12.426,28 12,27.575 12,27.052 L12,27 L10,27 L10,26 L12,26 L12,24 L10,24 L10,23 L12,23 L12,21 L10,21 L10,20 L12,20 L12,18 L10,18 L10,17 L12,17 L12,15 L10,15 L10,14 L12,14 L12,13.948 C12,13.425 12.426,13 12.948,13 L13,13 L13,11 L14,11 L14,13 L16,13 L16,11 L17,11 L17,13 L19,13 L19,11 L20,11 L20,13 L22,13 L22,11 L23,11 L23,13 L25,13 L25,11 L26,11 L26,13 L26.052,13 C26.574,13 27,13.425 27,13.948 L27,14 Z M21,33 L7,33 L7,19 L9,19 L9,18 L7.062,18 C6.477,18 6,18.477 6,19.062 L6,32.938 C6,33.523 6.477,34 7.062,34 L20.939,34 C21.524,34 22,33.523 22,32.938 L22,31 L21,31 L21,33 Z M34,7.062 L34,20.938 C34,21.523 33.524,22 32.939,22 L30,22 L30,21 L33,21 L33,7 L19,7 L19,10 L18,10 L18,7.062 C18,6.477 18.477,6 19.062,6 L32.939,6 C33.524,6 34,6.477 34,7.062 L34,7.062 Z\" id=\"Amazon-EC2_Icon_32_Squid\" fill=\"#FFFFFF\"\u003e\u003c/path\u003e \u003c/g\u003e\u003c/svg\u003e", | ||
| "svgComplete": "\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\u003c!DOCTYPE svg\u003e\u003csvg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 40 40\" height=\"20\" width=\"20\"\u003e \u003cg xmlns=\"http://www.w3.org/2000/svg\" id=\"Icon-Architecture/32/Arch_Amazon-EC2_32\" stroke=\"none\" stroke-width=\"1\" fill=\"none\" fill-rule=\"evenodd\"\u003e \u003cg xmlns=\"http://www.w3.org/2000/svg\" id=\"Icon-Architecture-BG/32/Compute\" fill=\"#ED7100\"\u003e \u003crect xmlns=\"http://www.w3.org/2000/svg\" id=\"Rectangle\" x=\"0\" y=\"0\" width=\"40\" height=\"40\"\u003e\u003c/rect\u003e \u003c/g\u003e \u003cpath xmlns=\"http://www.w3.org/2000/svg\" d=\"M26.052,27 L26,13.948 L13,14 L13,27.052 L26.052,27 Z M27,14 L29,14 L29,15 L27,15 L27,17 L29,17 L29,18 L27,18 L27,20 L29,20 L29,21 L27,21 L27,23 L29,23 L29,24 L27,24 L27,26 L29,26 L29,27 L27,27 L27,27.052 C27,27.575 26.574,28 26.052,28 L26,28 L26,30 L25,30 L25,28 L23,28 L23,30 L22,30 L22,28 L20,28 L20,30 L19,30 L19,28 L17,28 L17,30 L16,30 L16,28 L14,28 L14,30 L13,30 L13,28 L12.948,28 C12.426,28 12,27.575 12,27.052 L12,27 L10,27 L10,26 L12,26 L12,24 L10,24 L10,23 L12,23 L12,21 L10,21 L10,20 L12,20 L12,18 L10,18 L10,17 L12,17 L12,15 L10,15 L10,14 L12,14 L12,13.948 C12,13.425 12.426,13 12.948,13 L13,13 L13,11 L14,11 L14,13 L16,13 L16,11 L17,11 L17,13 L19,13 L19,11 L20,11 L20,13 L22,13 L22,11 L23,11 L23,13 L25,13 L25,11 L26,11 L26,13 L26.052,13 C26.574,13 27,13.425 27,13.948 L27,14 Z M21,33 L7,33 L7,19 L9,19 L9,18 L7.062,18 C6.477,18 6,18.477 6,19.062 L6,32.938 C6,33.523 6.477,34 7.062,34 L20.939,34 C21.524,34 22,33.523 22,32.938 L22,31 L21,31 L21,33 Z M34,7.062 L34,20.938 C34,21.523 33.524,22 32.939,22 L30,22 L30,21 L33,21 L33,7 L19,7 L19,10 L18,10 L18,7.062 C18,6.477 18.477,6 19.062,6 L32.939,6 C33.524,6 34,6.477 34,7.062 L34,7.062 Z\" id=\"Amazon-EC2_Icon_32_Squid\" fill=\"#FFFFFF\"\u003e\u003c/path\u003e \u003c/g\u003e\u003c/svg\u003e", | ||
| "svgWhite": "\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\u003c!DOCTYPE svg\u003e\u003csvg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 40 40\" height=\"20\" width=\"20\"\u003e \u003cg xmlns=\"http://www.w3.org/2000/svg\" id=\"Icon-Architecture/32/Arch_Amazon-EC2_32\" stroke=\"none\" stroke-width=\"1\" fill=\"none\" fill-rule=\"evenodd\"\u003e \u003cpath xmlns=\"http://www.w3.org/2000/svg\" d=\"M26.052,27 L26,13.948 L13,14 L13,27.052 L26.052,27 Z M27,14 L29,14 L29,15 L27,15 L27,17 L29,17 L29,18 L27,18 L27,20 L29,20 L29,21 L27,21 L27,23 L29,23 L29,24 L27,24 L27,26 L29,26 L29,27 L27,27 L27,27.052 C27,27.575 26.574,28 26.052,28 L26,28 L26,30 L25,30 L25,28 L23,28 L23,30 L22,30 L22,28 L20,28 L20,30 L19,30 L19,28 L17,28 L17,30 L16,30 L16,28 L14,28 L14,30 L13,30 L13,28 L12.948,28 C12.426,28 12,27.575 12,27.052 L12,27 L10,27 L10,26 L12,26 L12,24 L10,24 L10,23 L12,23 L12,21 L10,21 L10,20 L12,20 L12,18 L10,18 L10,17 L12,17 L12,15 L10,15 L10,14 L12,14 L12,13.948 C12,13.425 12.426,13 12.948,13 L13,13 L13,11 L14,11 L14,13 L16,13 L16,11 L17,11 L17,13 L19,13 L19,11 L20,11 L20,13 L22,13 L22,11 L23,11 L23,13 L25,13 L25,11 L26,11 L26,13 L26.052,13 C26.574,13 27,13.425 27,13.948 L27,14 Z M21,33 L7,33 L7,19 L9,19 L9,18 L7.062,18 C6.477,18 6,18.477 6,19.062 L6,32.938 C6,33.523 6.477,34 7.062,34 L20.939,34 C21.524,34 22,33.523 22,32.938 L22,31 L21,31 L21,33 Z M34,7.062 L34,20.938 C34,21.523 33.524,22 32.939,22 L30,22 L30,21 L33,21 L33,7 L19,7 L19,10 L18,10 L18,7.062 C18,6.477 18.477,6 19.062,6 L32.939,6 C33.524,6 34,6.477 34,7.062 L34,7.062 Z\" id=\"Amazon-EC2_Icon_32_Squid\" fill=\"#FFFFFF\"\u003e\u003c/path\u003e \u003c/g\u003e\u003c/svg\u003e" | ||
| }, | ||
| "model": { | ||
| "version": "v1.4.1" | ||
| }, | ||
| "components": [], | ||
| "relationships": [] | ||
| } |
There was a problem hiding this comment.
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.