/
schema_validate.go
54 lines (47 loc) · 1.43 KB
/
schema_validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package schema_validate
import (
"fmt"
"io/ioutil"
"strings"
"github.com/raid-codex/tools/utils"
"github.com/xeipuuv/gojsonschema"
"gopkg.in/alecthomas/kingpin.v2"
)
type Command struct {
File *string
SchemaFile *string
Definitions *string
}
func New(cmd *kingpin.CmdClause) *Command {
return &Command{
File: cmd.Flag("file", "Filename to check").Required().String(),
SchemaFile: cmd.Flag("schema-file", "Filename for the schema").Required().String(),
Definitions: cmd.Flag("definitions", "Filename for schema definitions").Required().String(),
}
}
func (c *Command) Run() {
definitions, err := ioutil.ReadFile(*c.Definitions)
if err != nil {
utils.Exit(1, err)
}
schema, err := ioutil.ReadFile(*c.SchemaFile)
if err != nil {
utils.Exit(1, err)
}
schemaString := strings.Replace(string(schema), `"definitions": {}`, fmt.Sprintf(`"definitions": %s`, string(definitions)), 1)
schemaLoader := gojsonschema.NewStringLoader(schemaString)
documentLoader := gojsonschema.NewReferenceLoader(*c.File)
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
utils.Exit(1, fmt.Errorf("cannot validate: %s", err))
}
if result.Valid() {
fmt.Printf("The document is valid\n")
} else {
errMessage := "The document is not valid. see errors :\n"
for _, desc := range result.Errors() {
errMessage = fmt.Sprintf("%s - %s\n", errMessage, desc)
}
utils.Exit(1, fmt.Errorf(errMessage))
}
}