1
1
package commands
2
2
3
3
import (
4
+ "encoding/json"
4
5
"errors"
5
6
"fmt"
6
7
"net/http"
@@ -11,8 +12,7 @@ import (
11
12
12
13
hcversion "github.com/hashicorp/go-version"
13
14
"github.com/pelletier/go-toml/v2"
14
- "github.com/santhosh-tekuri/jsonschema/v5"
15
- "github.com/santhosh-tekuri/jsonschema/v5/httploader"
15
+ "github.com/santhosh-tekuri/jsonschema/v6"
16
16
"github.com/spf13/cobra"
17
17
"github.com/spf13/pflag"
18
18
"gopkg.in/yaml.v3"
@@ -45,7 +45,7 @@ func (c *configCommand) executeVerify(cmd *cobra.Command, _ []string) error {
45
45
46
46
detail := v .DetailedOutput ()
47
47
48
- printValidationDetail (cmd , & detail )
48
+ printValidationDetail (cmd , detail )
49
49
50
50
return errors .New ("the configuration contains invalid elements" )
51
51
}
@@ -100,10 +100,13 @@ 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
+ loader := jsonschema.SchemeURLLoader {
105
+ "file" : jsonschema.FileLoader {},
106
+ "https" : newHTTPURLLoader (),
107
+ }
108
+ compiler .UseLoader (loader )
109
+ compiler .DefaultDraft (jsonschema .Draft7 )
107
110
108
111
schema , err := compiler .Compile (schemaPath )
109
112
if err != nil {
@@ -133,10 +136,11 @@ func validateConfiguration(schemaPath, targetFile string) error {
133
136
return schema .Validate (m )
134
137
}
135
138
136
- func printValidationDetail (cmd * cobra.Command , detail * jsonschema.Detailed ) {
137
- if detail .Error != "" {
139
+ func printValidationDetail (cmd * cobra.Command , detail * jsonschema.OutputUnit ) {
140
+ if detail .Error != nil {
141
+ b , _ := json .Marshal (detail .Error )
138
142
cmd .PrintErrf ("jsonschema: %q does not validate with %q: %s\n " ,
139
- strings .ReplaceAll (strings .TrimPrefix (detail .InstanceLocation , "/" ), "/" , "." ), detail .KeywordLocation , detail . Error )
143
+ strings .ReplaceAll (strings .TrimPrefix (detail .InstanceLocation , "/" ), "/" , "." ), detail .KeywordLocation , b )
140
144
}
141
145
142
146
for _ , d := range detail .Errors {
@@ -177,3 +181,26 @@ func decodeTomlFile(filename string) (any, error) {
177
181
178
182
return m , nil
179
183
}
184
+
185
+ type httpURLLoader http.Client
186
+
187
+ func newHTTPURLLoader () * httpURLLoader {
188
+ httpLoader := httpURLLoader (http.Client {
189
+ Timeout : 2 * time .Second ,
190
+ })
191
+ return & httpLoader
192
+ }
193
+
194
+ func (l * httpURLLoader ) Load (url string ) (any , error ) {
195
+ client := (* http .Client )(l )
196
+ resp , err := client .Get (url )
197
+ if err != nil {
198
+ return nil , err
199
+ }
200
+ defer resp .Body .Close ()
201
+ if resp .StatusCode != http .StatusOK {
202
+ return nil , fmt .Errorf ("%s returned status code %d" , url , resp .StatusCode )
203
+ }
204
+
205
+ return jsonschema .UnmarshalJSON (resp .Body )
206
+ }
0 commit comments