From 1db50f546810633e38f8f160c54119ba0ef6fda5 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 5 Jun 2016 23:50:50 -0700 Subject: [PATCH] schema/validate: Allow schema identifiers to contain a URL scheme And only fall back to assuming they're a local file if they don't already contain :// (which is unlikely to show up in local paths). This makes it easy to validate configurations against different versions of the JSON Schema (e.g. v0.5.0 or v1.0.0-rc1). Signed-off-by: W. Trevor King --- schema/README.md | 6 ++++++ schema/validate.go | 16 +++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/schema/README.md b/schema/README.md index 01a3e5ed8..6bdd00a29 100644 --- a/schema/README.md +++ b/schema/README.md @@ -37,3 +37,9 @@ Then use it like: ```bash ./validate schema.json /config.json ``` + +Or like: + +```bash +./validate https://raw.githubusercontent.com/opencontainers/runtime-spec/v1.0.0-rc1/schema/schema.json /config.json +``` diff --git a/schema/validate.go b/schema/validate.go index 34125cefd..48dd67e1a 100644 --- a/schema/validate.go +++ b/schema/validate.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" "github.com/xeipuuv/gojsonschema" ) @@ -16,12 +17,17 @@ func main() { os.Exit(1) } - schemaPath, err := filepath.Abs(os.Args[1]) - if err != nil { - fmt.Println(err) - os.Exit(1) + schemaPath := os.Args[1] + if !strings.Contains(schemaPath, "://") { + schemaPath, err := filepath.Abs(schemaPath) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + schemaPath = "file://" + schemaPath } - schemaLoader := gojsonschema.NewReferenceLoader("file://" + schemaPath) + schemaLoader := gojsonschema.NewReferenceLoader(schemaPath) + var documentLoader gojsonschema.JSONLoader if nargs > 1 {