Skip to content
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

build(deps): bump santhosh-tekuri/jsonschema/v5 to v6 #5171

Merged
merged 5 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading