Skip to content

Commit b3e367e

Browse files
committed
cmd/validate: refactor/simplify validator code.
Update validator to reuse code distilled into the schema package-local library. Signed-off-by: Krisztian Litkey <[email protected]>
1 parent 711b138 commit b3e367e

File tree

3 files changed

+52
-67
lines changed

3 files changed

+52
-67
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ test-schema: bin/validate
7979
# dependencies
8080
#
8181

82-
bin/validate: cmd/validate/validate.go
82+
bin/validate: cmd/validate/validate.go $(wildcard schema/*.json)
8383

8484
# quasi-automatic dependency for bin/cdi
8585
bin/cdi: $(wildcard cmd/cdi/*.go cmd/cdi/cmd/*.go) $(shell \

cmd/validate/validate.go

Lines changed: 48 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/*
2-
* From: https://github.com/opencontainers/runtime-spec/blob/643c1429d905bba70fe977bae274f367ad101e73/schema/validate.go
2+
* Original code from: https://github.com/opencontainers/runtime-spec/blob/643c1429d905bba70fe977bae274f367ad101e73/schema/validate.go
33
* Changes:
44
* - Output errors to stderr
5+
* - Refactored to use package-internal validation library
56
*
67
* Licensed under the Apache License, Version 2.0 (the "License");
78
* you may not use this file except in compliance with the License.
@@ -19,100 +20,83 @@
1920
package main
2021

2122
import (
23+
"flag"
2224
"fmt"
2325
"io/ioutil"
2426
"os"
25-
"path/filepath"
26-
"strings"
2727

28-
"github.com/xeipuuv/gojsonschema"
28+
"github.com/container-orchestrated-devices/container-device-interface/schema"
2929
)
3030

3131
const usage = `Validate is used to check document with specified schema.
3232
You can use validate in following ways:
3333
3434
1.specify document file as an argument
35-
validate <schema.json> <document.json>
35+
validate --schema <schema.json> <document.json>
3636
3737
2.pass document content through a pipe
38-
cat <document.json> | validate <schema.json>
38+
cat <document.json> | validate --schema <schema.json>
3939
4040
3.input document content manually, ended with ctrl+d(or your self-defined EOF keys)
41-
validate <schema.json>
41+
validate --schema <schema.json>
4242
[INPUT DOCUMENT CONTENT HERE]
4343
`
4444

4545
func main() {
46-
nargs := len(os.Args[1:])
47-
if nargs == 0 || nargs > 2 {
48-
fmt.Printf("ERROR: invalid arguments number\n\n%s\n", usage)
49-
os.Exit(1)
46+
var (
47+
schemaFile string
48+
docFile string
49+
docData []byte
50+
err error
51+
exitCode int
52+
)
53+
54+
flag.Usage = func() {
55+
fmt.Fprintf(flag.CommandLine.Output(), "%s\n", usage)
56+
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
57+
flag.PrintDefaults()
5058
}
5159

52-
if os.Args[1] == "help" ||
53-
os.Args[1] == "--help" ||
54-
os.Args[1] == "-h" {
55-
fmt.Printf("%s\n", usage)
56-
os.Exit(1)
57-
}
60+
flag.StringVar(&schemaFile, "schema", "builtin", "JSON Schema to validate against")
61+
flag.Parse()
5862

59-
schemaPath := os.Args[1]
60-
if !strings.Contains(schemaPath, "://") {
61-
var err error
62-
schemaPath, err = formatFilePath(schemaPath)
63+
if schemaFile != "" {
64+
scm, err := schema.Load(schemaFile)
6365
if err != nil {
64-
fmt.Printf("ERROR: invalid schema-file path: %s\n", err)
66+
fmt.Fprintf(os.Stderr, "failed to load schema %s: %v\n", schemaFile, err)
6567
os.Exit(1)
6668
}
67-
schemaPath = "file://" + schemaPath
68-
}
69-
70-
schemaLoader := gojsonschema.NewReferenceLoader(schemaPath)
71-
72-
var documentLoader gojsonschema.JSONLoader
73-
74-
if nargs > 1 {
75-
documentPath, err := formatFilePath(os.Args[2])
76-
if err != nil {
77-
fmt.Printf("ERROR: invalid document-file path: %s\n", err)
78-
os.Exit(1)
79-
}
80-
documentLoader = gojsonschema.NewReferenceLoader("file://" + documentPath)
69+
schema.Set(scm)
70+
fmt.Printf("Validating against JSON schema %s...\n", schemaFile)
8171
} else {
82-
documentBytes, err := ioutil.ReadAll(os.Stdin)
83-
if err != nil {
84-
fmt.Println(err)
85-
os.Exit(1)
86-
}
87-
documentString := string(documentBytes)
88-
documentLoader = gojsonschema.NewStringLoader(documentString)
72+
fmt.Printf("Validating against builtin JSON schema...\n")
8973
}
9074

91-
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
92-
if err != nil {
93-
fmt.Println(err)
94-
os.Exit(1)
75+
docs := flag.Args()
76+
if len(docs) == 0 {
77+
docs = []string{"-"}
9578
}
9679

97-
if result.Valid() {
98-
fmt.Printf("The document is valid\n")
99-
} else {
100-
fmt.Fprintf(os.Stderr, "The document is not valid. see errors :\n")
101-
for _, desc := range result.Errors() {
102-
fmt.Fprintf(os.Stderr, "- %s\n", desc)
80+
for _, docFile = range docs {
81+
if docFile == "" || docFile == "-" {
82+
docFile = "<stdin>"
83+
docData, err = ioutil.ReadAll(os.Stdin)
84+
if err != nil {
85+
fmt.Fprintf(os.Stderr, "failed to read document data from stdin: %v\n", err)
86+
os.Exit(1)
87+
}
88+
err = schema.ValidateData(docData)
89+
} else {
90+
err = schema.ValidateFile(docFile)
10391
}
104-
os.Exit(1)
105-
}
106-
}
10792

108-
func formatFilePath(path string) (string, error) {
109-
if _, err := os.Stat(path); err != nil {
110-
return "", err
93+
if err != nil {
94+
fmt.Fprintf(os.Stderr, "%s: validation failed:\n %v\n", docFile, err)
95+
exitCode = 1
96+
} else {
97+
fmt.Printf("%s: document is valid.\n", docFile)
98+
}
11199
}
112100

113-
absPath, err := filepath.Abs(path)
114-
if err != nil {
115-
return "", err
116-
}
117-
return absPath, nil
101+
os.Exit(exitCode)
118102
}

schema/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
VALIDATE ?= ../bin/validate
2+
SCHEMA ?= schema.json
23

34
test:
45
@FMT_RED=$$(tput setaf 1); \
@@ -7,7 +8,7 @@ test:
78
echo "Running Good Tests"; \
89
for FILE in $$(ls "testdata/good"); do \
910
FILE_PATH="testdata/good/$${FILE}"; \
10-
if $(VALIDATE) "schema.json" "$${FILE_PATH}" > /dev/null ; then \
11+
if $(VALIDATE) --schema "$(SCHEMA)" "$${FILE_PATH}" > /dev/null ; then \
1112
printf '%s[OK]%s %s\n' "$${FMT_BLUE}" "$${FMT_CLEAR}" "$${FILE_PATH}"; \
1213
else \
1314
printf '%s[KO]%s %s\n' "$${FMT_RED}" "$${FMT_CLEAR}" "$${FILE_PATH}"; \
@@ -17,7 +18,7 @@ test:
1718
echo "Running Bad Tests"; \
1819
for FILE in $$(ls "testdata/bad"); do \
1920
FILE_PATH="testdata/bad/$${FILE}"; \
20-
if $(VALIDATE) "schema.json" "$${FILE_PATH}" > /dev/null ; then \
21+
if $(VALIDATE) --schema "$(SCHEMA)" "$${FILE_PATH}" > /dev/null ; then \
2122
printf '%s[KO]%s %s\n' "$${FMT_RED}" "$${FMT_CLEAR}" "$${FILE_PATH}"; \
2223
exit 1; \
2324
else \

0 commit comments

Comments
 (0)