|
1 | 1 | package commands
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + "encoding/json" |
4 | 6 | "errors"
|
5 | 7 | "fmt"
|
6 | 8 | "net/http"
|
7 | 9 | "os"
|
8 | 10 | "path/filepath"
|
| 11 | + "strconv" |
9 | 12 | "strings"
|
10 | 13 | "time"
|
11 | 14 |
|
12 | 15 | hcversion "github.com/hashicorp/go-version"
|
13 | 16 | "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" |
16 | 18 | "github.com/spf13/cobra"
|
17 | 19 | "github.com/spf13/pflag"
|
18 | 20 | "gopkg.in/yaml.v3"
|
@@ -43,9 +45,7 @@ func (c *configCommand) executeVerify(cmd *cobra.Command, _ []string) error {
|
43 | 45 | return fmt.Errorf("[%s] validate: %w", usedConfigFile, err)
|
44 | 46 | }
|
45 | 47 |
|
46 |
| - detail := v.DetailedOutput() |
47 |
| - |
48 |
| - printValidationDetail(cmd, &detail) |
| 48 | + printValidationDetail(cmd, v.DetailedOutput()) |
49 | 49 |
|
50 | 50 | return errors.New("the configuration contains invalid elements")
|
51 | 51 | }
|
@@ -100,10 +100,12 @@ func createSchemaURL(flags *pflag.FlagSet, buildInfo BuildInfo) (string, error)
|
100 | 100 | }
|
101 | 101 |
|
102 | 102 | func validateConfiguration(schemaPath, targetFile string) error {
|
103 |
| - httploader.Client = &http.Client{Timeout: 2 * time.Second} |
104 |
| - |
105 | 103 | 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) |
107 | 109 |
|
108 | 110 | schema, err := compiler.Compile(schemaPath)
|
109 | 111 | if err != nil {
|
@@ -133,10 +135,13 @@ func validateConfiguration(schemaPath, targetFile string) error {
|
133 | 135 | return schema.Validate(m)
|
134 | 136 | }
|
135 | 137 |
|
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 | + |
138 | 143 | 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) |
140 | 145 | }
|
141 | 146 |
|
142 | 147 | for _, d := range detail.Errors {
|
@@ -177,3 +182,33 @@ func decodeTomlFile(filename string) (any, error) {
|
177 | 182 |
|
178 | 183 | return m, nil
|
179 | 184 | }
|
| 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 | +} |
0 commit comments