-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Create validator
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"path" | ||
|
||
"github.com/gobuffalo/packr" | ||
"github.com/xeipuuv/gojsonschema" | ||
) | ||
|
||
func main() { | ||
var doc string | ||
flag.StringVar(&doc, "doc", "examples/invalid/invalid_email.json", "Example file") | ||
flag.Parse() | ||
ValidateJSON(doc) | ||
} | ||
|
||
// ValidateJSON is used to check for validity | ||
func ValidateJSON(doc string) { | ||
|
||
file := path.Join("file:///", GetPath(), "/", doc) | ||
box := packr.NewBox("./") | ||
s := box.String("schema.json") | ||
schemaLoader := gojsonschema.NewStringLoader(s) | ||
documentLoader := gojsonschema.NewReferenceLoader(file) | ||
result, err := gojsonschema.Validate(schemaLoader, documentLoader) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
if result.Valid() { | ||
fmt.Printf("The document is valid\n") | ||
} else { | ||
fmt.Printf("The document is not valid. see errors :\n") | ||
for _, desc := range result.Errors() { | ||
fmt.Printf("- %s\n", desc) | ||
} | ||
} | ||
|
||
} | ||
|
||
// GetPath is used to get current path | ||
func GetPath() string { | ||
dir, err := os.Getwd() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return dir | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.