Skip to content

Commit

Permalink
build(deps): bump santhosh-tekuri/jsonschema/v5 to v6 (#5171)
Browse files Browse the repository at this point in the history
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
  • Loading branch information
alexandear and ldez authored Nov 29, 2024
1 parent 6b000ab commit bab25b4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 46 additions & 11 deletions pkg/commands/config_verify.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package commands

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"

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"
Expand Down Expand Up @@ -43,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")
}
Expand Down Expand Up @@ -100,10 +100,12 @@ 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
compiler.UseLoader(jsonschema.SchemeURLLoader{
"file": jsonschema.FileLoader{},
"https": newJSONSchemaHTTPLoader(),
})
compiler.DefaultDraft(jsonschema.Draft7)

schema, err := compiler.Compile(schemaPath)
if err != nil {
Expand Down Expand Up @@ -133,10 +135,13 @@ 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 {
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, detail.Error)
strings.ReplaceAll(strings.TrimPrefix(detail.InstanceLocation, "/"), "/", "."), detail.KeywordLocation, details)
}

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

return m, nil
}

type jsonschemaHTTPLoader struct {
*http.Client
}

func newJSONSchemaHTTPLoader() *jsonschemaHTTPLoader {
return &jsonschemaHTTPLoader{Client: &http.Client{
Timeout: 2 * time.Second,
}}
}

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)
}

return jsonschema.UnmarshalJSON(resp.Body)
}
10 changes: 7 additions & 3 deletions test/configuration_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -68,8 +68,12 @@ 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)
}

Expand Down

0 comments on commit bab25b4

Please sign in to comment.