Repository navigation
schema/validate: migrate to santhosh-tekuri/jsonschema/v6 #1327
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
Open
thaJeztah
wants to merge
1
commit into
opencontainers:main
Choose a base branch
from
thaJeztah:migrate_validate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,6 @@ | ||
| github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
| github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
| github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
| github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= | ||
| github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
| github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c= | ||
| github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= | ||
| github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0= | ||
| github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ= | ||
| github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74= | ||
| github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= | ||
| github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI= | ||
| github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= | ||
| github.com/santhosh-tekuri/jsonschema/v6 v6.0.3 h1:1EYB5IzjZawrrnELUi78f9fPu57HuXjmddZPjrls/28= | ||
| github.com/santhosh-tekuri/jsonschema/v6 v6.0.3/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU= | ||
| golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= | ||
| golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,11 +3,14 @@ package main | |
| import ( | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "os" | ||
| "path/filepath" | ||
| "runtime" | ||
| "strings" | ||
|
|
||
| "github.com/xeipuuv/gojsonschema" | ||
| "github.com/santhosh-tekuri/jsonschema/v6" | ||
| ) | ||
|
|
||
| const usage = `Validate is used to check document with specified schema. | ||
|
|
@@ -24,6 +27,21 @@ You can use validate in following ways: | |
| [INPUT DOCUMENT CONTENT HERE] | ||
| ` | ||
|
|
||
| type httpURLLoader struct{} | ||
|
|
||
| func (httpURLLoader) Load(url string) (any, error) { | ||
| resp, err := http.Get(url) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer func() { _ = resp.Body.Close() }() | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, fmt.Errorf("unexpected HTTP status: %s", resp.Status) | ||
| } | ||
| return jsonschema.UnmarshalJSON(resp.Body) | ||
| } | ||
|
|
||
| func main() { | ||
| nargs := len(os.Args[1:]) | ||
| if nargs == 0 || nargs > 2 { | ||
|
|
@@ -41,50 +59,55 @@ func main() { | |
| schemaPath := os.Args[1] | ||
| if !strings.Contains(schemaPath, "://") { | ||
| var err error | ||
| schemaPath, err = formatFilePath(schemaPath) | ||
| schemaPath, err = formatFileURL(schemaPath) | ||
| if err != nil { | ||
| fmt.Printf("ERROR: invalid schema-file path: %s\n", err) | ||
| os.Exit(1) | ||
| } | ||
| schemaPath = "file://" + schemaPath | ||
| } | ||
|
|
||
| schemaLoader := gojsonschema.NewReferenceLoader(schemaPath) | ||
| compiler := jsonschema.NewCompiler() | ||
| compiler.DefaultDraft(jsonschema.Draft4) | ||
| compiler.UseLoader(jsonschema.SchemeURLLoader{ | ||
| "file": jsonschema.FileLoader{}, | ||
| "http": httpURLLoader{}, | ||
| "https": httpURLLoader{}, | ||
| }) | ||
|
|
||
| var documentLoader gojsonschema.JSONLoader | ||
| schema, err := compiler.Compile(schemaPath) | ||
|
thaJeztah marked this conversation as resolved.
thaJeztah marked this conversation as resolved.
|
||
| if err != nil { | ||
| fmt.Println(err) | ||
|
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. nit: |
||
| os.Exit(1) | ||
| } | ||
|
|
||
| var document io.Reader = os.Stdin | ||
| if nargs > 1 { | ||
| documentPath, err := formatFilePath(os.Args[2]) | ||
| if err != nil { | ||
| fmt.Printf("ERROR: invalid document-file path: %s\n", err) | ||
| os.Exit(1) | ||
| } | ||
| documentLoader = gojsonschema.NewReferenceLoader("file://" + documentPath) | ||
| } else { | ||
| documentBytes, err := io.ReadAll(os.Stdin) | ||
| f, err := os.Open(documentPath) | ||
| if err != nil { | ||
| fmt.Println(err) | ||
| os.Exit(1) | ||
| } | ||
| documentString := string(documentBytes) | ||
| documentLoader = gojsonschema.NewStringLoader(documentString) | ||
| defer func() { _ = f.Close() }() | ||
| document = f | ||
| } | ||
|
|
||
| result, err := gojsonschema.Validate(schemaLoader, documentLoader) | ||
| instance, err := jsonschema.UnmarshalJSON(document) | ||
| if err != nil { | ||
| fmt.Println(err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| if result.Valid() { | ||
| fmt.Printf("The document is valid\n") | ||
| } else { | ||
| fmt.Printf("The document is not valid. see errors :\n") | ||
| for _, desc := range result.Errors() { | ||
| fmt.Printf("- %s\n", desc) | ||
| } | ||
| if err := schema.Validate(instance); err != nil { | ||
| fmt.Println("The document is not valid. see errors :") | ||
| fmt.Println(err) | ||
| os.Exit(1) | ||
| } | ||
| fmt.Println("The document is valid") | ||
| } | ||
|
|
||
| func formatFilePath(path string) (string, error) { | ||
|
|
@@ -98,3 +121,18 @@ func formatFilePath(path string) (string, error) { | |
| } | ||
| return absPath, nil | ||
| } | ||
|
|
||
| func formatFileURL(path string) (string, error) { | ||
| fp, err := formatFilePath(path) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| path = filepath.ToSlash(fp) | ||
| if runtime.GOOS == "windows" { | ||
| // FileLoader removes one leading slash on Windows. | ||
| // Preserve both leading slashes for UNC paths. | ||
| path = "/" + path | ||
| } | ||
| return (&url.URL{Scheme: "file", Path: path}).String(), nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: maybe add a timeout here? Say