From 0e9ff5a6c97e4747fb962aa46d475bb2d972c742 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Fri, 29 Nov 2024 18:10:17 +0200 Subject: [PATCH 1/5] dev: bump santhosh-tekuri/jsonschema/v5 to v6 --- go.mod | 2 +- go.sum | 6 +++-- pkg/commands/config_verify.go | 45 ++++++++++++++++++++++++++------- test/configuration_file_test.go | 2 +- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 238ea62e93c7..a0bd6f1f48fe 100644 --- a/go.mod +++ b/go.mod @@ -90,7 +90,7 @@ require ( github.com/ryancurrah/gomodguard v1.3.5 github.com/ryanrolds/sqlclosecheck v0.5.1 github.com/sanposhiho/wastedassign/v2 v2.0.7 - github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 + github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 github.com/sashamelentyev/interfacebloat v1.1.0 github.com/sashamelentyev/usestdlibvars v1.27.0 github.com/securego/gosec/v2 v2.21.4 diff --git a/go.sum b/go.sum index 20925694f9da..5f8161ec676a 100644 --- a/go.sum +++ b/go.sum @@ -129,6 +129,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/denis-tingaikin/go-header v0.5.0 h1:SRdnP5ZKvcO9KKRP1KJrhFR3RrlGuD+42t4429eC9k8= github.com/denis-tingaikin/go-header v0.5.0/go.mod h1:mMenU5bWrok6Wl2UsZjy+1okegmwQ3UgWl4V1D8gjlY= +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/ebitengine/purego v0.8.1 h1:sdRKd6plj7KYW33EH5As6YKfe8m9zbN9JMrOjNVF/BE= github.com/ebitengine/purego v0.8.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -474,8 +476,8 @@ github.com/ryanrolds/sqlclosecheck v0.5.1 h1:dibWW826u0P8jNLsLN+En7+RqWWTYrjCB9f github.com/ryanrolds/sqlclosecheck v0.5.1/go.mod h1:2g3dUjoS6AL4huFdv6wn55WpLIDjY7ZgUR4J8HOO/XQ= github.com/sanposhiho/wastedassign/v2 v2.0.7 h1:J+6nrY4VW+gC9xFzUc+XjPD3g3wF3je/NsJFwFK7Uxc= github.com/sanposhiho/wastedassign/v2 v2.0.7/go.mod h1:KyZ0MWTwxxBmfwn33zh3k1dmsbF2ud9pAAGfoLfjhtI= -github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4= -github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY= +github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 h1:PKK9DyHxif4LZo+uQSgXNqs0jj5+xZwwfKHgph2lxBw= +github.com/santhosh-tekuri/jsonschema/v6 v6.0.1/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU= github.com/sashamelentyev/interfacebloat v1.1.0 h1:xdRdJp0irL086OyW1H/RTZTr1h/tMEOsumirXcOJqAw= github.com/sashamelentyev/interfacebloat v1.1.0/go.mod h1:+Y9yU5YdTkrNvoX0xHc84dxiN1iBi9+G8zZIhPVoNjQ= github.com/sashamelentyev/usestdlibvars v1.27.0 h1:t/3jZpSXtRPRf2xr0m63i32ZrusyurIGT9E5wAvXQnI= diff --git a/pkg/commands/config_verify.go b/pkg/commands/config_verify.go index 89017e9bfc07..4925018c9f11 100644 --- a/pkg/commands/config_verify.go +++ b/pkg/commands/config_verify.go @@ -1,6 +1,7 @@ package commands import ( + "encoding/json" "errors" "fmt" "net/http" @@ -11,8 +12,7 @@ import ( hcversion "github.com/hashicorp/go-version" "github.com/pelletier/go-toml/v2" - "github.com/santhosh-tekuri/jsonschema/v5" - "github.com/santhosh-tekuri/jsonschema/v5/httploader" + "github.com/santhosh-tekuri/jsonschema/v6" "github.com/spf13/cobra" "github.com/spf13/pflag" "gopkg.in/yaml.v3" @@ -45,7 +45,7 @@ func (c *configCommand) executeVerify(cmd *cobra.Command, _ []string) error { detail := v.DetailedOutput() - printValidationDetail(cmd, &detail) + printValidationDetail(cmd, detail) return errors.New("the configuration contains invalid elements") } @@ -100,10 +100,13 @@ func createSchemaURL(flags *pflag.FlagSet, buildInfo BuildInfo) (string, error) } func validateConfiguration(schemaPath, targetFile string) error { - httploader.Client = &http.Client{Timeout: 2 * time.Second} - compiler := jsonschema.NewCompiler() - compiler.Draft = jsonschema.Draft7 + loader := jsonschema.SchemeURLLoader{ + "file": jsonschema.FileLoader{}, + "https": newHTTPURLLoader(), + } + compiler.UseLoader(loader) + compiler.DefaultDraft(jsonschema.Draft7) schema, err := compiler.Compile(schemaPath) if err != nil { @@ -133,10 +136,11 @@ func validateConfiguration(schemaPath, targetFile string) error { return schema.Validate(m) } -func printValidationDetail(cmd *cobra.Command, detail *jsonschema.Detailed) { - if detail.Error != "" { +func printValidationDetail(cmd *cobra.Command, detail *jsonschema.OutputUnit) { + if detail.Error != nil { + b, _ := json.Marshal(detail.Error) cmd.PrintErrf("jsonschema: %q does not validate with %q: %s\n", - strings.ReplaceAll(strings.TrimPrefix(detail.InstanceLocation, "/"), "/", "."), detail.KeywordLocation, detail.Error) + strings.ReplaceAll(strings.TrimPrefix(detail.InstanceLocation, "/"), "/", "."), detail.KeywordLocation, b) } for _, d := range detail.Errors { @@ -177,3 +181,26 @@ func decodeTomlFile(filename string) (any, error) { return m, nil } + +type httpURLLoader http.Client + +func newHTTPURLLoader() *httpURLLoader { + httpLoader := httpURLLoader(http.Client{ + Timeout: 2 * time.Second, + }) + return &httpLoader +} + +func (l *httpURLLoader) Load(url string) (any, error) { + client := (*http.Client)(l) + resp, err := client.Get(url) + if err != nil { + return nil, err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("%s returned status code %d", url, resp.StatusCode) + } + + return jsonschema.UnmarshalJSON(resp.Body) +} diff --git a/test/configuration_file_test.go b/test/configuration_file_test.go index 83cd46a6dcce..79dd492effb5 100644 --- a/test/configuration_file_test.go +++ b/test/configuration_file_test.go @@ -9,7 +9,7 @@ import ( "strings" "testing" - "github.com/santhosh-tekuri/jsonschema/v5" + "github.com/santhosh-tekuri/jsonschema/v6" "github.com/stretchr/testify/require" "gopkg.in/yaml.v3" ) From acfd4df887aecb919586706c8cc1be82478b4160 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 29 Nov 2024 18:44:29 +0100 Subject: [PATCH 2/5] review --- pkg/commands/config_verify.go | 26 +++++++++++++++++--------- test/configuration_file_test.go | 6 +++--- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/pkg/commands/config_verify.go b/pkg/commands/config_verify.go index 4925018c9f11..469a68bd99ee 100644 --- a/pkg/commands/config_verify.go +++ b/pkg/commands/config_verify.go @@ -1,6 +1,7 @@ package commands import ( + "context" "encoding/json" "errors" "fmt" @@ -103,7 +104,7 @@ func validateConfiguration(schemaPath, targetFile string) error { compiler := jsonschema.NewCompiler() loader := jsonschema.SchemeURLLoader{ "file": jsonschema.FileLoader{}, - "https": newHTTPURLLoader(), + "https": newJSONSchemaHTTPLoader(), } compiler.UseLoader(loader) compiler.DefaultDraft(jsonschema.Draft7) @@ -182,22 +183,29 @@ func decodeTomlFile(filename string) (any, error) { return m, nil } -type httpURLLoader http.Client +type jsonschemaHTTPLoader struct { + *http.Client +} -func newHTTPURLLoader() *httpURLLoader { - httpLoader := httpURLLoader(http.Client{ +func newJSONSchemaHTTPLoader() *jsonschemaHTTPLoader { + return &jsonschemaHTTPLoader{Client: &http.Client{ Timeout: 2 * time.Second, - }) - return &httpLoader + }} } -func (l *httpURLLoader) Load(url string) (any, error) { - client := (*http.Client)(l) - resp, err := client.Get(url) +func (l jsonschemaHTTPLoader) Load(url string) (any, error) { + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, http.NoBody) if err != nil { return nil, err } + + resp, err := l.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("%s returned status code %d", url, resp.StatusCode) } diff --git a/test/configuration_file_test.go b/test/configuration_file_test.go index 79dd492effb5..6c6352cea282 100644 --- a/test/configuration_file_test.go +++ b/test/configuration_file_test.go @@ -15,7 +15,7 @@ import ( ) func Test_validateTestConfigurationFiles(t *testing.T) { - err := validateTestConfigurationFiles("../jsonschema/golangci.next.jsonschema.json", ".") + err := validateTestConfigurationFiles("../../jsonschema/golangci.next.jsonschema.json", ".") require.NoError(t, err) } @@ -59,7 +59,7 @@ func validateTestConfigurationFiles(schemaPath, targetDir string) error { func loadSchema(schemaPath string) (*jsonschema.Schema, error) { compiler := jsonschema.NewCompiler() - compiler.Draft = jsonschema.Draft7 + compiler.DefaultDraft(jsonschema.Draft7) schemaFile, err := os.Open(schemaPath) if err != nil { @@ -73,7 +73,7 @@ func loadSchema(schemaPath string) (*jsonschema.Schema, error) { return nil, fmt.Errorf("add schema resource: %w", err) } - schema, err := compiler.Compile(filepath.Base(schemaPath)) + schema, err := compiler.Compile(schemaPath) if err != nil { return nil, fmt.Errorf("compile schema: %w", err) } From cb958f1ebdde70fdc0d61d7db506ab3bb97f6c26 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 29 Nov 2024 19:00:48 +0100 Subject: [PATCH 3/5] review --- pkg/commands/config_verify.go | 16 ++++++++-------- test/configuration_file_test.go | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/commands/config_verify.go b/pkg/commands/config_verify.go index 469a68bd99ee..5a26c75aed8f 100644 --- a/pkg/commands/config_verify.go +++ b/pkg/commands/config_verify.go @@ -8,6 +8,7 @@ import ( "net/http" "os" "path/filepath" + "strconv" "strings" "time" @@ -44,9 +45,7 @@ func (c *configCommand) executeVerify(cmd *cobra.Command, _ []string) error { return fmt.Errorf("[%s] validate: %w", usedConfigFile, err) } - detail := v.DetailedOutput() - - printValidationDetail(cmd, detail) + printValidationDetail(cmd, v.DetailedOutput()) return errors.New("the configuration contains invalid elements") } @@ -102,11 +101,10 @@ func createSchemaURL(flags *pflag.FlagSet, buildInfo BuildInfo) (string, error) func validateConfiguration(schemaPath, targetFile string) error { compiler := jsonschema.NewCompiler() - loader := jsonschema.SchemeURLLoader{ + compiler.UseLoader(jsonschema.SchemeURLLoader{ "file": jsonschema.FileLoader{}, "https": newJSONSchemaHTTPLoader(), - } - compiler.UseLoader(loader) + }) compiler.DefaultDraft(jsonschema.Draft7) schema, err := compiler.Compile(schemaPath) @@ -139,9 +137,11 @@ func validateConfiguration(schemaPath, targetFile string) error { func printValidationDetail(cmd *cobra.Command, detail *jsonschema.OutputUnit) { if detail.Error != nil { - b, _ := json.Marshal(detail.Error) + data, _ := json.Marshal(detail.Error) + details, _ := strconv.Unquote(string(data)) + cmd.PrintErrf("jsonschema: %q does not validate with %q: %s\n", - strings.ReplaceAll(strings.TrimPrefix(detail.InstanceLocation, "/"), "/", "."), detail.KeywordLocation, b) + strings.ReplaceAll(strings.TrimPrefix(detail.InstanceLocation, "/"), "/", "."), detail.KeywordLocation, details) } for _, d := range detail.Errors { diff --git a/test/configuration_file_test.go b/test/configuration_file_test.go index 6c6352cea282..994b6ae4acdf 100644 --- a/test/configuration_file_test.go +++ b/test/configuration_file_test.go @@ -15,7 +15,7 @@ import ( ) func Test_validateTestConfigurationFiles(t *testing.T) { - err := validateTestConfigurationFiles("../../jsonschema/golangci.next.jsonschema.json", ".") + err := validateTestConfigurationFiles("../jsonschema/golangci.next.jsonschema.json", ".") require.NoError(t, err) } From b92fe2c86f30dd7ec5a61493ab11d04b8053e2ac Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 29 Nov 2024 19:10:01 +0100 Subject: [PATCH 4/5] review --- test/configuration_file_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/configuration_file_test.go b/test/configuration_file_test.go index 994b6ae4acdf..a4009a4dea3d 100644 --- a/test/configuration_file_test.go +++ b/test/configuration_file_test.go @@ -68,12 +68,16 @@ func loadSchema(schemaPath string) (*jsonschema.Schema, error) { defer func() { _ = schemaFile.Close() }() - err = compiler.AddResource(filepath.Base(schemaPath), schemaFile) + doc, err := jsonschema.UnmarshalJSON(schemaFile) if err != nil { + return nil, fmt.Errorf("unmarshal schema resource: %w", err) + } + + if err := compiler.AddResource(filepath.Base(schemaPath), doc); err != nil { return nil, fmt.Errorf("add schema resource: %w", err) } - schema, err := compiler.Compile(schemaPath) + schema, err := compiler.Compile(filepath.Base(schemaPath)) if err != nil { return nil, fmt.Errorf("compile schema: %w", err) } From f79021d250576f2c81dce951168ebb3c7644464f Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 29 Nov 2024 19:12:40 +0100 Subject: [PATCH 5/5] review --- test/configuration_file_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/configuration_file_test.go b/test/configuration_file_test.go index a4009a4dea3d..6601b050fa22 100644 --- a/test/configuration_file_test.go +++ b/test/configuration_file_test.go @@ -73,7 +73,7 @@ func loadSchema(schemaPath string) (*jsonschema.Schema, error) { return nil, fmt.Errorf("unmarshal schema resource: %w", err) } - if err := compiler.AddResource(filepath.Base(schemaPath), doc); err != nil { + if err = compiler.AddResource(filepath.Base(schemaPath), doc); err != nil { return nil, fmt.Errorf("add schema resource: %w", err) }