Skip to content
Closed
Changes from 2 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
57 changes: 37 additions & 20 deletions cmd/jv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"

"github.com/santhosh-tekuri/jsonschema/v5"
Expand All @@ -21,49 +22,65 @@ func usage() {
flag.PrintDefaults()
}

var (
validDrafts = map[int]*jsonschema.Draft{
4: jsonschema.Draft4,
6: jsonschema.Draft6,
7: jsonschema.Draft7,
2019: jsonschema.Draft2019,
2020: jsonschema.Draft2020,
}
validOutputs = []string{"flag", "basic", "detailed"}
)

func main() {
draft := flag.Int("draft", 2020, "draft used when '$schema' attribute is missing. valid values 4, 5, 7, 2019, 2020")
output := flag.String("output", "", "output format. valid values flag, basic, detailed")
drafts := func() string {
ds := make([]int, 0, len(validDrafts))
for d := range validDrafts {
ds = append(ds, d)
}
sort.Ints(ds)
var b strings.Builder
for i, d := range ds {
if i != 0 {
b.WriteString(", ")
}
fmt.Fprintf(&b, "%d", d)
}
return b.String()
}()
draft := flag.Int("draft", 2020, "draft used when '$schema' attribute is missing. valid values "+drafts)
output := flag.String("output", "", "output format. valid values "+strings.Join(validOutputs, ", "))
assertFormat := flag.Bool("assertformat", false, "enable format assertions with draft >= 2019")
assertContent := flag.Bool("assertcontent", false, "enable content assertions with draft >= 2019")
flag.Usage = usage
flag.Parse()
if len(flag.Args()) == 0 {
usage()
os.Exit(1)
os.Exit(2)
}

compiler := jsonschema.NewCompiler()
switch *draft {
case 4:
compiler.Draft = jsonschema.Draft4
case 6:
compiler.Draft = jsonschema.Draft6
case 7:
compiler.Draft = jsonschema.Draft7
case 2019:
compiler.Draft = jsonschema.Draft2019
case 2020:
compiler.Draft = jsonschema.Draft2020
default:
fmt.Fprintln(os.Stderr, "draft must be 4, 5, 7, 2019 or 2020")
os.Exit(1)
var ok bool
if compiler.Draft, ok = validDrafts[*draft]; !ok {
fmt.Fprintln(os.Stderr, "draft must be one of", drafts)
os.Exit(2)
}

compiler.LoadURL = loadURL
compiler.AssertFormat = *assertFormat
compiler.AssertContent = *assertContent

var validOutput bool
for _, out := range []string{"", "flag", "basic", "detailed"} {
for _, out := range append(validOutputs, "") {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if out~="" instead of append; this makes it clear

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I quite got what you meant, but 8ffba32 at least simplifies it some.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to say add if guard as shown below:

if *output != "" {
    valid := false
    for _, out := range validOutputs {
        If *output == out {
            valid = true
            break
        }
     }
     If !valid {
         fmt.Fprintln(os.Stderr, "output must be one of", strings.Join(validOutputs, ", "))
         os.Exit(2)
     }
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b9a55db

if *output == out {
validOutput = true
break
}
}
if !validOutput {
fmt.Fprintln(os.Stderr, "output must be flag, basic or detailed")
os.Exit(1)
fmt.Fprintln(os.Stderr, "output must be one of", strings.Join(validOutputs, ", "))
os.Exit(2)
}

schema, err := compiler.Compile(flag.Arg(0))
Expand Down