-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
323 lines (283 loc) · 8.03 KB
/
main.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// yajsv is a command line tool for validating JSON and YAML documents against
// a provided JSON Schema - https://json-schema.org/
package main
//go:generate go run gen_testdata.go
import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/unicode"
"github.com/ghodss/yaml"
"github.com/mitchellh/go-homedir"
"github.com/xeipuuv/gojsonschema"
)
var (
version = "v1.4.0-dev"
schemaFlag = flag.String("s", "", "primary JSON schema to validate against, required")
quietFlag = flag.Bool("q", false, "quiet, only print validation failures and errors")
versionFlag = flag.Bool("v", false, "print version and exit")
bomFlag = flag.Bool("b", false, "allow BOM in JSON files, error if seen and unset")
listFlags stringFlags
refFlags stringFlags
)
// https://en.wikipedia.org/wiki/Byte_order_mark#Byte_order_marks_by_encoding
const (
bomUTF8 = "\xEF\xBB\xBF"
bomUTF16BE = "\xFE\xFF"
bomUTF16LE = "\xFF\xFE"
)
var (
encUTF16BE = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)
encUTF16LE = unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)
)
func init() {
flag.Var(&listFlags, "l", "validate JSON documents from newline separated paths and/or globs in a text file (relative to the basename of the file itself)")
flag.Var(&refFlags, "r", "referenced schema(s), can be globs and/or used multiple times")
flag.Usage = printUsage
}
func main() {
log.SetFlags(0)
os.Exit(realMain(os.Args[1:], os.Stdout))
}
func realMain(args []string, w io.Writer) int {
flag.CommandLine.Parse(args)
if *versionFlag {
fmt.Fprintln(w, version)
return 0
}
if *schemaFlag == "" {
return usageError("missing required -s schema argument")
}
// Resolve document paths to validate
docs := make([]string, 0)
for _, arg := range flag.Args() {
docs = append(docs, glob(arg)...)
}
for _, list := range listFlags {
dir := filepath.Dir(list)
f, err := os.Open(list)
if err != nil {
return schemaError("%s: %s", list, err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
// Calclate the glob relative to the directory of the file list
pattern := strings.TrimSpace(scanner.Text())
if !filepath.IsAbs(pattern) {
pattern = filepath.Join(dir, pattern)
}
docs = append(docs, glob(pattern)...)
}
if err := scanner.Err(); err != nil {
return schemaError("%s: invalid file list: %s", list, err)
}
}
if len(docs) == 0 {
return usageError("no documents to validate")
}
// Compile target schema
sl := gojsonschema.NewSchemaLoader()
schemaPath, err := filepath.Abs(*schemaFlag)
if err != nil {
return schemaError("%s: unable to convert to absolute path: %s", *schemaFlag, err)
}
for _, ref := range refFlags {
for _, p := range glob(ref) {
absPath, err := filepath.Abs(p)
if err != nil {
return schemaError("%s: unable to convert to absolute path: %s", absPath, err)
}
if absPath == schemaPath {
continue
}
loader, err := jsonLoader(absPath)
if err != nil {
return schemaError("%s: unable to load schema ref: %s", *schemaFlag, err)
}
if err := sl.AddSchemas(loader); err != nil {
return schemaError("%s: invalid schema: %s", p, err)
}
}
}
schemaLoader, err := jsonLoader(schemaPath)
if err != nil {
return schemaError("%s: unable to load schema: %s", *schemaFlag, err)
}
schema, err := sl.Compile(schemaLoader)
if err != nil {
return schemaError("%s: invalid schema: %s", *schemaFlag, err)
}
// Validate the schema against each doc in parallel, limiting simultaneous
// open files to avoid ulimit issues.
var wg sync.WaitGroup
sem := make(chan int, runtime.GOMAXPROCS(0)+10)
failures := make([]string, 0)
errors := make([]string, 0)
for _, p := range docs {
wg.Add(1)
go func(path string) {
defer wg.Done()
sem <- 0
defer func() { <-sem }()
loader, err := jsonLoader(path)
if err != nil {
msg := fmt.Sprintf("%s: error: load doc: %s", path, err)
fmt.Fprintln(w, msg)
errors = append(errors, msg)
return
}
result, err := schema.Validate(loader)
switch {
case err != nil:
msg := fmt.Sprintf("%s: error: validate: %s", path, err)
fmt.Fprintln(w, msg)
errors = append(errors, msg)
case !result.Valid():
lines := make([]string, len(result.Errors()))
for i, desc := range result.Errors() {
lines[i] = fmt.Sprintf("%s: fail: %s", path, desc)
}
msg := strings.Join(lines, "\n")
fmt.Fprintln(w, msg)
failures = append(failures, msg)
case !*quietFlag:
fmt.Fprintf(w, "%s: pass\n", path)
}
}(p)
}
wg.Wait()
// Summarize results (e.g. errors)
if !*quietFlag {
if len(failures) > 0 {
fmt.Fprintf(w, "%d of %d failed validation\n", len(failures), len(docs))
fmt.Fprintln(w, strings.Join(failures, "\n"))
}
if len(errors) > 0 {
fmt.Fprintf(w, "%d of %d malformed documents\n", len(errors), len(docs))
fmt.Fprintln(w, strings.Join(errors, "\n"))
}
}
exit := 0
if len(failures) > 0 {
exit |= 1
}
if len(errors) > 0 {
exit |= 2
}
return exit
}
func jsonLoader(path string) (gojsonschema.JSONLoader, error) {
buf, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
switch filepath.Ext(path) {
case ".yml", ".yaml":
// TODO YAML requires the precense of a BOM to detect UTF-16
// text. Is there a decent hueristic to detect UTF-16 text
// missing a BOM so we can provide a better error message?
buf, err = yaml.YAMLToJSON(buf)
default:
buf, err = jsonDecodeCharset(buf)
}
if err != nil {
return nil, err
}
// TODO What if we have an empty document?
return gojsonschema.NewBytesLoader(buf), nil
}
// jsonDecodeCharset attempts to detect UTF-16 (LE or BE) JSON text and
// decode as appropriate. It also skips a BOM at the start of the buffer
// if `-b` was specified. Presence of a BOM is an error otherwise.
func jsonDecodeCharset(buf []byte) ([]byte, error) {
if len(buf) < 2 { // UTF-8
return buf, nil
}
bom := ""
var enc encoding.Encoding
switch {
case bytes.HasPrefix(buf, []byte(bomUTF8)):
bom = bomUTF8
case bytes.HasPrefix(buf, []byte(bomUTF16BE)):
bom = bomUTF16BE
enc = encUTF16BE
case bytes.HasPrefix(buf, []byte(bomUTF16LE)):
bom = bomUTF16LE
enc = encUTF16LE
case buf[0] == 0:
enc = encUTF16BE
case buf[1] == 0:
enc = encUTF16LE
}
if bom != "" {
if !*bomFlag {
return nil, fmt.Errorf("unexpected BOM, see `-b` flag")
}
buf = buf[len(bom):]
}
if enc != nil {
return enc.NewDecoder().Bytes(buf)
}
return buf, nil
}
func printUsage() {
fmt.Fprintf(os.Stderr, `Usage: %s -s schema.(json|yml) [options] document.(json|yml) ...
yajsv validates JSON and YAML document(s) against a schema. One of three status
results are reported per document:
pass: Document is valid relative to the schema
fail: Document is invalid relative to the schema
error: Document is malformed, e.g. not valid JSON or YAML
The 'fail' status may be reported multiple times per-document, once for each
schema validation failure.
Sets the exit code to 1 on any failures, 2 on any errors, 3 on both, 4 on
invalid usage, 5 on schema definition or file-list errors. Otherwise, 0 is
returned if everything passes validation.
Options:
`, os.Args[0])
flag.PrintDefaults()
fmt.Fprintln(os.Stderr)
}
func usageError(msg string) int {
fmt.Fprintln(os.Stderr, msg)
printUsage()
return 4
}
func schemaError(format string, args ...interface{}) int {
fmt.Fprintf(os.Stderr, format+"\n", args...)
return 5
}
// glob is a wrapper that also resolves `~` since we may be skipping
// the shell expansion when single-quoting globs at the command line
func glob(pattern string) []string {
pattern, err := homedir.Expand(pattern)
if err != nil {
log.Fatal(err)
}
paths, err := filepath.Glob(pattern)
if err != nil {
log.Fatal(err)
}
if len(paths) == 0 {
log.Fatalf("%s: no such file or directory", pattern)
}
return paths
}
type stringFlags []string
func (sf *stringFlags) String() string {
return "multi-string"
}
func (sf *stringFlags) Set(value string) error {
*sf = append(*sf, value)
return nil
}