File tree Expand file tree Collapse file tree 1 file changed +22
-9
lines changed Expand file tree Collapse file tree 1 file changed +22
-9
lines changed Original file line number Diff line number Diff line change @@ -2,15 +2,17 @@ package main
22
33import (
44 "fmt"
5+ "io/ioutil"
56 "os"
67 "path/filepath"
78
89 "github.com/xeipuuv/gojsonschema"
910)
1011
1112func main () {
12- if len (os .Args [1 :]) != 2 {
13- fmt .Printf ("ERROR: usage is: %s <schema.json> <config.json>\n " , os .Args [0 ])
13+ nargs := len (os .Args [1 :])
14+ if nargs == 0 || nargs > 2 {
15+ fmt .Printf ("ERROR: usage is: %s <schema.json> [<document.json>]\n " , os .Args [0 ])
1416 os .Exit (1 )
1517 }
1618
@@ -19,14 +21,25 @@ func main() {
1921 fmt .Println (err )
2022 os .Exit (1 )
2123 }
22- documentPath , err := filepath .Abs (os .Args [2 ])
23- if err != nil {
24- fmt .Println (err )
25- os .Exit (1 )
26- }
27-
2824 schemaLoader := gojsonschema .NewReferenceLoader ("file://" + schemaPath )
29- documentLoader := gojsonschema .NewReferenceLoader ("file://" + documentPath )
25+ var documentLoader gojsonschema.JSONLoader
26+
27+ if nargs > 1 {
28+ documentPath , err := filepath .Abs (os .Args [2 ])
29+ if err != nil {
30+ fmt .Println (err )
31+ os .Exit (1 )
32+ }
33+ documentLoader = gojsonschema .NewReferenceLoader ("file://" + documentPath )
34+ } else {
35+ documentBytes , err := ioutil .ReadAll (os .Stdin )
36+ if err != nil {
37+ fmt .Println (err )
38+ os .Exit (1 )
39+ }
40+ documentString := string (documentBytes )
41+ documentLoader = gojsonschema .NewStringLoader (documentString )
42+ }
3043
3144 result , err := gojsonschema .Validate (schemaLoader , documentLoader )
3245 if err != nil {
You can’t perform that action at this time.
0 commit comments