11package commands
22
33import (
4+ "encoding/json"
45 "errors"
56 "fmt"
67 "net/http"
@@ -11,8 +12,7 @@ import (
1112
1213 hcversion "github.com/hashicorp/go-version"
1314 "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"
1616 "github.com/spf13/cobra"
1717 "github.com/spf13/pflag"
1818 "gopkg.in/yaml.v3"
@@ -45,7 +45,7 @@ func (c *configCommand) executeVerify(cmd *cobra.Command, _ []string) error {
4545
4646 detail := v .DetailedOutput ()
4747
48- printValidationDetail (cmd , & detail )
48+ printValidationDetail (cmd , detail )
4949
5050 return errors .New ("the configuration contains invalid elements" )
5151 }
@@ -100,10 +100,13 @@ func createSchemaURL(flags *pflag.FlagSet, buildInfo BuildInfo) (string, error)
100100}
101101
102102func 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+ loader := jsonschema.SchemeURLLoader {
105+ "file" : jsonschema.FileLoader {},
106+ "https" : newHTTPURLLoader (),
107+ }
108+ compiler .UseLoader (loader )
109+ compiler .DefaultDraft (jsonschema .Draft7 )
107110
108111 schema , err := compiler .Compile (schemaPath )
109112 if err != nil {
@@ -133,10 +136,11 @@ func validateConfiguration(schemaPath, targetFile string) error {
133136 return schema .Validate (m )
134137}
135138
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 )
138142 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 )
140144 }
141145
142146 for _ , d := range detail .Errors {
@@ -177,3 +181,26 @@ func decodeTomlFile(filename string) (any, error) {
177181
178182 return m , nil
179183}
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