Skip to content

Commit bab25b4

Browse files
alexandearldez
andauthored
build(deps): bump santhosh-tekuri/jsonschema/v5 to v6 (#5171)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent 6b000ab commit bab25b4

File tree

4 files changed

+58
-17
lines changed

4 files changed

+58
-17
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ require (
9090
github.com/ryancurrah/gomodguard v1.3.5
9191
github.com/ryanrolds/sqlclosecheck v0.5.1
9292
github.com/sanposhiho/wastedassign/v2 v2.0.7
93-
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
93+
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1
9494
github.com/sashamelentyev/interfacebloat v1.1.0
9595
github.com/sashamelentyev/usestdlibvars v1.27.0
9696
github.com/securego/gosec/v2 v2.21.4

go.sum

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/commands/config_verify.go

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package commands
22

33
import (
4+
"context"
5+
"encoding/json"
46
"errors"
57
"fmt"
68
"net/http"
79
"os"
810
"path/filepath"
11+
"strconv"
912
"strings"
1013
"time"
1114

1215
hcversion "github.com/hashicorp/go-version"
1316
"github.com/pelletier/go-toml/v2"
14-
"github.com/santhosh-tekuri/jsonschema/v5"
15-
"github.com/santhosh-tekuri/jsonschema/v5/httploader"
17+
"github.com/santhosh-tekuri/jsonschema/v6"
1618
"github.com/spf13/cobra"
1719
"github.com/spf13/pflag"
1820
"gopkg.in/yaml.v3"
@@ -43,9 +45,7 @@ func (c *configCommand) executeVerify(cmd *cobra.Command, _ []string) error {
4345
return fmt.Errorf("[%s] validate: %w", usedConfigFile, err)
4446
}
4547

46-
detail := v.DetailedOutput()
47-
48-
printValidationDetail(cmd, &detail)
48+
printValidationDetail(cmd, v.DetailedOutput())
4949

5050
return errors.New("the configuration contains invalid elements")
5151
}
@@ -100,10 +100,12 @@ func createSchemaURL(flags *pflag.FlagSet, buildInfo BuildInfo) (string, error)
100100
}
101101

102102
func validateConfiguration(schemaPath, targetFile string) error {
103-
httploader.Client = &http.Client{Timeout: 2 * time.Second}
104-
105103
compiler := jsonschema.NewCompiler()
106-
compiler.Draft = jsonschema.Draft7
104+
compiler.UseLoader(jsonschema.SchemeURLLoader{
105+
"file": jsonschema.FileLoader{},
106+
"https": newJSONSchemaHTTPLoader(),
107+
})
108+
compiler.DefaultDraft(jsonschema.Draft7)
107109

108110
schema, err := compiler.Compile(schemaPath)
109111
if err != nil {
@@ -133,10 +135,13 @@ func validateConfiguration(schemaPath, targetFile string) error {
133135
return schema.Validate(m)
134136
}
135137

136-
func printValidationDetail(cmd *cobra.Command, detail *jsonschema.Detailed) {
137-
if detail.Error != "" {
138+
func printValidationDetail(cmd *cobra.Command, detail *jsonschema.OutputUnit) {
139+
if detail.Error != nil {
140+
data, _ := json.Marshal(detail.Error)
141+
details, _ := strconv.Unquote(string(data))
142+
138143
cmd.PrintErrf("jsonschema: %q does not validate with %q: %s\n",
139-
strings.ReplaceAll(strings.TrimPrefix(detail.InstanceLocation, "/"), "/", "."), detail.KeywordLocation, detail.Error)
144+
strings.ReplaceAll(strings.TrimPrefix(detail.InstanceLocation, "/"), "/", "."), detail.KeywordLocation, details)
140145
}
141146

142147
for _, d := range detail.Errors {
@@ -177,3 +182,33 @@ func decodeTomlFile(filename string) (any, error) {
177182

178183
return m, nil
179184
}
185+
186+
type jsonschemaHTTPLoader struct {
187+
*http.Client
188+
}
189+
190+
func newJSONSchemaHTTPLoader() *jsonschemaHTTPLoader {
191+
return &jsonschemaHTTPLoader{Client: &http.Client{
192+
Timeout: 2 * time.Second,
193+
}}
194+
}
195+
196+
func (l jsonschemaHTTPLoader) Load(url string) (any, error) {
197+
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, http.NoBody)
198+
if err != nil {
199+
return nil, err
200+
}
201+
202+
resp, err := l.Do(req)
203+
if err != nil {
204+
return nil, err
205+
}
206+
207+
defer resp.Body.Close()
208+
209+
if resp.StatusCode != http.StatusOK {
210+
return nil, fmt.Errorf("%s returned status code %d", url, resp.StatusCode)
211+
}
212+
213+
return jsonschema.UnmarshalJSON(resp.Body)
214+
}

test/configuration_file_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"strings"
1010
"testing"
1111

12-
"github.com/santhosh-tekuri/jsonschema/v5"
12+
"github.com/santhosh-tekuri/jsonschema/v6"
1313
"github.com/stretchr/testify/require"
1414
"gopkg.in/yaml.v3"
1515
)
@@ -59,7 +59,7 @@ func validateTestConfigurationFiles(schemaPath, targetDir string) error {
5959

6060
func loadSchema(schemaPath string) (*jsonschema.Schema, error) {
6161
compiler := jsonschema.NewCompiler()
62-
compiler.Draft = jsonschema.Draft7
62+
compiler.DefaultDraft(jsonschema.Draft7)
6363

6464
schemaFile, err := os.Open(schemaPath)
6565
if err != nil {
@@ -68,8 +68,12 @@ func loadSchema(schemaPath string) (*jsonschema.Schema, error) {
6868

6969
defer func() { _ = schemaFile.Close() }()
7070

71-
err = compiler.AddResource(filepath.Base(schemaPath), schemaFile)
71+
doc, err := jsonschema.UnmarshalJSON(schemaFile)
7272
if err != nil {
73+
return nil, fmt.Errorf("unmarshal schema resource: %w", err)
74+
}
75+
76+
if err = compiler.AddResource(filepath.Base(schemaPath), doc); err != nil {
7377
return nil, fmt.Errorf("add schema resource: %w", err)
7478
}
7579

0 commit comments

Comments
 (0)