Skip to content

Commit

Permalink
Merge pull request opencontainers#482 from wking/validate-stdin
Browse files Browse the repository at this point in the history
schema/validate: Support reading documents via stdin
  • Loading branch information
Michael Crosby committed Jun 3, 2016
2 parents 831d961 + 8ca7174 commit 08c556f
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions schema/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/xeipuuv/gojsonschema"
)

func main() {
if len(os.Args[1:]) != 2 {
fmt.Printf("ERROR: usage is: %s <schema.json> <config.json>\n", os.Args[0])
nargs := len(os.Args[1:])
if nargs == 0 || nargs > 2 {
fmt.Printf("ERROR: usage is: %s <schema.json> [<document.json>]\n", os.Args[0])
os.Exit(1)
}

Expand All @@ -19,14 +21,25 @@ func main() {
fmt.Println(err)
os.Exit(1)
}
documentPath, err := filepath.Abs(os.Args[2])
if err != nil {
fmt.Println(err)
os.Exit(1)
}

schemaLoader := gojsonschema.NewReferenceLoader("file://" + schemaPath)
documentLoader := gojsonschema.NewReferenceLoader("file://" + documentPath)
var documentLoader gojsonschema.JSONLoader

if nargs > 1 {
documentPath, err := filepath.Abs(os.Args[2])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
documentLoader = gojsonschema.NewReferenceLoader("file://" + documentPath)
} else {
documentBytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
documentString := string(documentBytes)
documentLoader = gojsonschema.NewStringLoader(documentString)
}

result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
Expand Down

0 comments on commit 08c556f

Please sign in to comment.