validate can now read remote schemas

and also resolve remote refs within those schemas. so now i can write
proper, composable/reusable stuff
main
Nick Dumas 2 years ago
parent 30f3093f22
commit 114d67874c

@ -4,18 +4,31 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
package cmd package cmd
import ( import (
"bytes"
"fmt" "fmt"
"io" "io"
"log" "log"
"net/url"
"os" "os"
"strings"
"github.com/santhosh-tekuri/jsonschema/v5" "github.com/santhosh-tekuri/jsonschema/v5"
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
func LoadURL(s string) (io.ReadCloser, error) {
u, err := url.Parse(s)
if err != nil {
return nil, err
}
loader, ok := jsonschema.Loaders[u.Scheme]
if !ok {
return nil, jsonschema.LoaderNotFoundError(s)
}
return loader(s)
}
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
var validateCmd = &cobra.Command{ var validateCmd = &cobra.Command{
Use: "validate", Use: "validate",
@ -25,8 +38,8 @@ var validateCmd = &cobra.Command{
// Uncomment the following line if your bare application // Uncomment the following line if your bare application
// has an action associated with it: // has an action associated with it:
PreRunE: func(cmd *cobra.Command, args []string) error { PreRunE: func(cmd *cobra.Command, args []string) error {
schemaFilename := cmd.Flag("schema").Value.String() schemaURL := cmd.Flag("schema").Value.String()
if len(schemaFilename) == 0 { if len(schemaURL) == 0 {
return fmt.Errorf("Please profide a schema filename") return fmt.Errorf("Please profide a schema filename")
} }
target := cmd.Flag("target").Value.String() target := cmd.Flag("target").Value.String()
@ -37,14 +50,8 @@ var validateCmd = &cobra.Command{
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var m interface{} var m interface{}
var schemaBytes bytes.Buffer
schemaFilename := cmd.Flag("schema").Value.String() schemaURL := cmd.Flag("schema").Value.String()
schemaFile, err := os.Open(schemaFilename)
if err != nil {
log.Fatalf("could not open schema file: %s\n", err)
}
io.Copy(&schemaBytes, schemaFile)
// err := yaml.Unmarshal([]byte(yamlText), &m) // err := yaml.Unmarshal([]byte(yamlText), &m)
targetFilename := cmd.Flag("target").Value.String() targetFilename := cmd.Flag("target").Value.String()
@ -59,11 +66,7 @@ var validateCmd = &cobra.Command{
} }
compiler := jsonschema.NewCompiler() compiler := jsonschema.NewCompiler()
if err := compiler.AddResource(schemaFilename, strings.NewReader(schemaBytes.String())); err != nil { schema, err := compiler.Compile(schemaURL)
log.Fatalf("error adding resource to jsonschema compiler: %s\n", err)
}
schema, err := compiler.Compile(schemaFilename)
if err != nil { if err != nil {
log.Fatalf("error compiling schema: %s\n", err) log.Fatalf("error compiling schema: %s\n", err)
} }

Loading…
Cancel
Save