func BenchmarkParseAndMakeValidator(b *testing.B) { r := strings.NewReader(schemaJSON) for i := 0; i < b.N; i++ { s, _ := schema.Read(r) v := validator.New(s) v.Compile() // force compiling for comparison } }
func _main() int { if len(os.Args) < 2 { usage() return 1 } schemaf, err := os.Open(os.Args[1]) if err != nil { log.Printf("failed to open schema: %s", err) return 1 } defer schemaf.Close() s, err := schema.Read(schemaf) if err != nil { log.Printf("failed to read schema: %s", err) return 1 } if err := dumpJSON(s); err != nil { return 1 } if len(os.Args) < 3 { return 0 } f, err := os.Open(os.Args[2]) if err != nil { log.Printf("failed to open data: %s", err) return 1 } defer f.Close() in, err := ioutil.ReadAll(f) if err != nil { log.Printf("failed to read data: %s", err) return 1 } var v interface{} if err := json.Unmarshal(in, &v); err != nil { log.Printf("failed to decode data: %s", err) return 1 } valid := validator.New(s) if err := valid.Validate(v); err != nil { log.Printf("validation failed") return 1 } if err := dumpJSON(v); err != nil { return 1 } return 0 }
func TestMarshal(t *testing.T) { // roundTripSchemas are schemas that can be read and written back to the same // content. They also include an object that should match the schema to make // sure the schema is correctly written. var roundTripSchemas = []struct { Name string Schema string ValidValue interface{} }{{ Name: "Integer", Schema: `{ "type": "integer" }`, ValidValue: int(0), }, { Name: "String", Schema: `{ "type": "string" }`, ValidValue: "value", }, { Name: "Object", Schema: `{ "additionalProperties": false, "properties": { "attr": { "type": "integer" } }, "type": "object" }`, ValidValue: struct{ attr int }{10}, }} for _, definition := range roundTripSchemas { t.Logf("Testing schema %s", definition.Name) s, err := schema.Read(strings.NewReader(definition.Schema)) if !assert.NoError(t, err, "schema.Read should succeed") { return } v := validator.New(s) err = v.Validate(definition.ValidValue) if !assert.NoError(t, err, "ValidValue should Validate successfully") { return } output, err := json.MarshalIndent(s, "", " ") if !assert.NoError(t, err, "json.Marshal should succeed") { return } if !assert.Equal(t, definition.Schema, string(output), "json.Marshal should result in the same value as the input.") { return } } }
func Example() { s, err := schema.ReadFile("schema.json") if err != nil { log.Printf("failed to read schema: %s", err) return } for name, pdef := range s.Properties { // Do what you will with `pdef`, which contain // Schema information for `name` property _ = name _ = pdef } // Create a validator v := validator.New(s) // You can also validate an arbitrary piece of data var p interface{} // initialize using json.Unmarshal... if err := v.Validate(p); err != nil { log.Printf("failed to validate data: %s", err) } }
func TestValidate(t *testing.T) { tests := []string{ "allof", "anyof", "array", "arraylength", "arraytuple", "arraytuple_disallow_additional", "arrayunique", "boolean", "business", "integer", "not", "null", "numrange", "numrange_exclmax", "objectpatterns", "objectpropdepend", "objectpropsize", "objectproprequired", "oneof", "strlen", "strpattern", } for _, name := range tests { schemaf := filepath.Join("test", name+".json") t.Logf("Reading schema file %s", schemaf) schema, err := readSchema(schemaf) if !assert.NoError(t, err, "reading schema file %s should succeed", schemaf) { return } valid := validator.New(schema) pat := filepath.Join("test", fmt.Sprintf("%s_pass*.json", name)) files, _ := filepath.Glob(pat) for _, passf := range files { t.Logf("Testing schema against %s (expect to PASS)", passf) passin, err := os.Open(passf) if !assert.NoError(t, err, "os.Open(%s) should succeed", passf) { return } var m map[string]interface{} // XXX should test against structs if !assert.NoError(t, json.NewDecoder(passin).Decode(&m), "json.Decode should succeed") { return } if !assert.NoError(t, valid.Validate(m), "schema.Validate should succeed") { return } } pat = filepath.Join("test", fmt.Sprintf("%s_fail*.json", name)) files, _ = filepath.Glob(pat) for _, failf := range files { t.Logf("Testing schema against %s (expect to FAIL)", failf) failin, err := os.Open(failf) if !assert.NoError(t, err, "os.Open(%s) should succeed", failf) { return } var m map[string]interface{} // XXX should test against structs if !assert.NoError(t, json.NewDecoder(failin).Decode(&m), "json.Decode should succeed") { return } if !assert.Error(t, valid.Validate(m), "schema.Validate should fail") { return } } } }