// and define an implementation for that type that performs the schema validation func (r *schemaValidatorType) validateSchema(schema, cfg string) []serror.SnapError { schemaLoader := gojsonschema.NewStringLoader(schema) testDoc := gojsonschema.NewStringLoader(cfg) result, err := gojsonschema.Validate(schemaLoader, testDoc) var serrors []serror.SnapError // Check for invalid json if err != nil { serrors = append(serrors, serror.New(err)) return serrors } // check if result passes validation if result.Valid() { return nil } for _, err := range result.Errors() { serr := serror.New(errors.New("Validate schema error")) serr.SetFields(map[string]interface{}{ "value": err.Value(), "context": err.Context().String("::"), "description": err.Description(), }) serrors = append(serrors, serr) } return serrors }
//NewJSONEventTrackingValidator returns a new JSONEventTrackingValidator with the parsed Schema func NewJSONEventTrackingValidator() (*JSONEventTrackingValidator, error) { schema, err := gojsonschema.NewSchema(gojsonschema.NewStringLoader(schema)) if err != nil { return nil, err } return &JSONEventTrackingValidator{schema}, err }
// LoadSchema loads the schema from a string. func LoadSchema(schema string) error { var err error schemaLoader := gojsonschema.NewStringLoader(schema) movenSchemaValidator, err = gojsonschema.NewSchema(schemaLoader) return err }
func NewSchemaValidator(t *testing.T, schemaName string, got interface{}) *SchemaValidator { dir, err := os.Getwd() if err != nil { t.Fatal(err) } // Platform compatibility: use "/" separators always for file:// dir = filepath.ToSlash(dir) schema := gojsonschema.NewReferenceLoader(fmt.Sprintf( "file:///%s/schema/%s", dir, schemaName), ) marshalled, err := json.Marshal(got) if err != nil { t.Fatal(err) } subject := gojsonschema.NewStringLoader(string(marshalled)) return &SchemaValidator{ Schema: schema, Subject: subject, } }
func (v Validator) ServeHTTP(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { http.Error(w, http.StatusText(405), 405) return } var resp ValidationResult render := func() { if err := json.NewEncoder(w).Encode(resp); err != nil { http.Error(w, http.StatusText(500), 500) } } jsonDoc := r.FormValue("json") resp.DocType = r.FormValue("doctype") schema, ok := v[r.FormValue("doctype")] if !ok { resp.Errors = []string{fmt.Sprintf("This document type schema not yet implemented: %q", r.FormValue("doctype"))} render() return } loader := js.NewStringLoader(jsonDoc) result, err := schema.Validate(loader) if err != nil { resp.Errors = []string{"JSON is not well-formed: " + err.Error()} } else { if result.Valid() { resp.Valid = true } else { for _, err := range result.Errors() { msg := err.Context.String() + ": " + err.Description resp.Errors = append(resp.Errors, msg) } } } render() }
func BenchmarkGojsonschemaParse(b *testing.B) { l := gojsonschema.NewStringLoader(schemaJSON) for i := 0; i < b.N; i++ { s, _ := gojsonschema.NewSchema(l) _ = s } }
func (ts *ThreatSpec) Validate() error { schemaLoader := gojsonschema.NewStringLoader(ThreatSpecSchemaStrictv0) documentLoader := gojsonschema.NewStringLoader(ts.ToJson()) if result, err := gojsonschema.Validate(schemaLoader, documentLoader); err != nil { return err } else if result.Valid() { return nil } else { var errs []string for _, desc := range result.Errors() { errs = append(errs, fmt.Sprintf(" - %s", desc)) } return errors.New(strings.Join(errs, "\n")) } }
func MustSchema(schemaString string) *schema.Schema { s, err := schema.NewSchema(schema.NewStringLoader(schemaString)) if err != nil { panic(err) } return s }
//ValidateEventTrackingPayloadHandler validates that the payload has a valid JSON Schema //Uses a FinalHandler as next because it consumes the request's body func ValidateEventTrackingPayloadHandler(next *ctx.FinalHandler) *ctx.Handler { return ctx.NewHandler(next.Context, func(c *ctx.Context, w http.ResponseWriter, r *http.Request) { //Validate the payload body, err := ioutil.ReadAll(r.Body) if err != nil { c.Logger.Errorf("%s : Error reading body: %s", utilities.GetIP(r), err.Error()) http.Error(w, http.StatusText(500), 500) return } next.Payload = body requestLoader := gojsonschema.NewStringLoader(string(body)) result, err := c.JSONTrackingEventValidator.Schema.Validate(requestLoader) if err != nil { c.Logger.Infof("%s : Json validation error: %s", utilities.GetIP(r), err.Error()) http.Error(w, http.StatusText(400), 400) return } if !result.Valid() { c.Logger.Infof("%s : Payload is not valid: %+v", utilities.GetIP(r), result.Errors()) http.Error(w, http.StatusText(400), 400) return } next.ServeHTTP(w, r) }) }
func TestGenJsonSchema(t *testing.T) { str := ` { "k1":"a", "k2":1, "k3":[], "k4":["a"], "k5":[1], "k6":[1.1], "k7":{}, "k8":{"a":1}, "k9":{"a":1,"b":[]}, "k10":{"a":1,"b":[],"c":{"d":1.1}}, "k11":{"a":1,"b":[],"c":{"d":1.1,"f":["a"]}}, "k12":{"a":1,"b":[{"a":1,"b":[1]}]} } ` var obj interface{} err := json.Unmarshal([]byte(str), &obj) assert.Equal(t, err, nil) schema, err := GenJsonSchema(obj) assert.Equal(t, err, nil) _, err = json.MarshalIndent(schema, "", " ") assert.Equal(t, err, nil) // fmt.Println(string(bs)) goSchema, err := gojsonschema.NewSchema(gojsonschema.NewGoLoader(schema)) assert.Equal(t, err, nil) documentLoader := gojsonschema.NewStringLoader(str) ret, err := goSchema.Validate(documentLoader) assert.Equal(t, err, nil) assert.Equal(t, ret.Valid(), true) }
func validateFile(ctx Context, path string) { ctx.running <- true defer func() { ctx.running <- false }() //fmt.Println(path) bytes, err := ioutil.ReadFile(path) if err != nil { ctx.result <- fmt.Sprint(err) return } result, err := ctx.schema.Validate(gojsonschema.NewStringLoader(string(bytes))) if err != nil { ctx.result <- fmt.Sprint(path, err) return } if !result.Valid() { errors := result.Errors() res := make([]string, 1+len(errors)) res[0] = fmt.Sprintf("%s:", path) //ctx.result <- fmt.Sprintf("%s:\n", path) for i, desc := range errors { res[1+i] = fmt.Sprint(desc) } ctx.result <- strings.Join(res, "\n- ") + "\n" } else { //ctx.result <- = fmt.Sprintf("%s: ok\n", path) } }
func isValidJson(schema, json string) bool { schema = strings.TrimSpace(schema) if len(schema) == 2 { schema = `{"additionalProperties": false}` } else { schema = schema[:len(schema)-1] + `,"additionalProperties":false}` } dataLoader := gojsonschema.NewStringLoader(json) schemaLoader := gojsonschema.NewStringLoader(schema) if result, validateErr := gojsonschema.Validate(schemaLoader, dataLoader); validateErr == nil { return result.Valid() } else { return false } }
func (s *JSONSchema) ValidateContent(content []byte) (r *gojsonschema.Result, err error) { documentLoader := gojsonschema.NewStringLoader(string(content)) schema, err := s.getSchema() if err != nil { return } return schema.Validate(documentLoader) }
//middleware for json schema validation func JsonSchemaValidator() gin.HandlerFunc { return func(c *gin.Context) { contextCopy := c.Copy() var schema string validateSchema := true switch c.Request.RequestURI { case "/signup": schema = "signup.json" case "/login": schema = "login.json" default: validateSchema = false } if validateSchema { schemaLoader := gojsonschema.NewReferenceLoader("file://" + myConstants.JsonSchemasFilesLocation + "/" + schema) body, _ := ioutil.ReadAll(contextCopy.Request.Body) //fmt.Printf("%s", body) documentLoader := gojsonschema.NewStringLoader(string(body)) result, err := gojsonschema.Validate(schemaLoader, documentLoader) if err != nil { log.Fatalln(myMessages.JsonSchemaValidationError + err.Error()) } if !result.Valid() { //fmt.Printf("The document is not valid. see errors :\n") type errorsList []string type JsonErrorOutput struct { Error bool Message string Faults errorsList } var el errorsList for _, desc := range result.Errors() { //fmt.Printf("- %s\n", desc) el = append(el, fmt.Sprintf("%s", desc)) } var jeo JsonErrorOutput jeo.Error = true jeo.Message = myMessages.JsonSchemaValidationFailed jeo.Faults = el c.JSON(http.StatusBadRequest, jeo) c.Abort() return } } c.Next() } }
func validateJSON(jsonText string, schemaText string) error { schemaLoader := gojsonschema.NewStringLoader(schemaText) docLoader := gojsonschema.NewStringLoader(jsonText) result, err := gojsonschema.Validate(schemaLoader, docLoader) if err != nil { return err } if !result.Valid() { errMessage := "The JSON data is not valid:\n" for _, desc := range result.Errors() { errMessage += fmt.Sprintf(" %v\n", desc) } return errors.New(errMessage) } return nil }
// Read loadmodel from document - you can provide your own schema to validate the loadmodel. func (test *TestConfig) ReadConfigValidate(document string, schema string) error { documentLoader := gojsonschema.NewStringLoader(document) schemaLoader := gojsonschema.NewStringLoader(schema) result, err := gojsonschema.Validate(schemaLoader, documentLoader) if err != nil { return err } if !result.Valid() { msg := "the loadmodel is not valid:" for _, desc := range result.Errors() { msg += fmt.Sprintf("\n- %s", desc) } return fmt.Errorf(msg) } return json.Unmarshal([]byte(document), &test.config) }
func getSchema() (gojsonschema.JSONLoader, error) { if schema != nil { return schema, nil } b, err := Asset("schema.json") if err != nil { return nil, err } return gojsonschema.NewStringLoader(string(b)), nil }
func ParseSchema(path string, route *Route) error { content, err := ioutil.ReadFile(path + "/schemas/" + route.Name + ".schema") if err != nil { return nil } schema, err := gojsonschema.NewSchema(gojsonschema.NewStringLoader(string(content))) if err != nil { return err } route.Schema = schema return nil }
// ValidateJSON validates the given runtime against its defined schema func (cfg *RuntimeOptions) ValidateJSON() error { schema := gojson.NewStringLoader(RuntimeSchema) doc := gojson.NewGoLoader(cfg) if result, err := gojson.Validate(schema, doc); err != nil { return err } else if !result.Valid() { return combineErrors(result.Errors()) } return nil }
// Test that the burned in payload schema is a valid json schema func TestPayloadSchemaValid(t *testing.T) { payloadSchema := taskPayloadSchema() schemaLoader := gojsonschema.NewStringLoader(payloadSchema) _, err := gojsonschema.NewSchema(schemaLoader) if err != nil { t.Logf("Generic Worker payload schema is not a valid json schema for platform %v.", runtime.GOOS) t.Log("Payload schema:") t.Log(payloadSchema) t.Log("Error:") t.Fatalf("%s", err) } }
// Parse takes a state json and returns the state for it func Parse(b []byte) (*State, error) { schema, err := getSchema() if err != nil { return nil, err } result, err := gojsonschema.Validate(schema, gojsonschema.NewStringLoader(string(b))) if err != nil { return nil, err } if !result.Valid() { return nil, &SchemaError{result.Errors()} } var s State return &s, json.Unmarshal(b, &s) }
func ParseSchemaVersion(route *Route, path string, version int) error { content, err := ioutil.ReadFile(path) if err != nil { return err } schema, err := gojsonschema.NewSchema(gojsonschema.NewStringLoader(string(content))) if err != nil { return err } if route.Versions[version] == nil { route.Versions[version] = &RouteVersion{Version: version} } route.Versions[version].Schema = schema return nil }
func validateJSON(schema string, obj Entity) error { schemaObj := gojson.NewStringLoader(schema) doc := gojson.NewGoLoader(obj) if result, err := gojson.Validate(schemaObj, doc); err != nil { return err } else if !result.Valid() { var errors []string for _, err := range result.Errors() { errors = append(errors, fmt.Sprintf("%s\n", err)) } return errored.New(strings.Join(errors, "\n")) } return nil }
func main() { 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) } schemaPath, err := filepath.Abs(os.Args[1]) if err != nil { fmt.Println(err) os.Exit(1) } schemaLoader := gojsonschema.NewReferenceLoader("file://" + schemaPath) 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 { 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) } os.Exit(1) } }
func TestRouteSql(t *testing.T) { schemaContent := ` { "title": "Get user", "description": "Get user", "type": "object", "properties": { "id": { "description": "User id", "type": "integer" } }, "required": ["id"] } ` schema, err := gojsonschema.NewSchema(gojsonschema.NewStringLoader(string(schemaContent))) if err != nil { t.Errorf("Expected not to get error, but got: %v", err) } tmpl, err := makeTemplate(`select * from users where id={{.id}}`) if err != nil { t.Errorf("Expected not to get error, but got: %v", err) } route := &Route{ Versions: map[int]*RouteVersion{ 0: { Version: 0, SqlTemplate: tmpl, Schema: schema, }, }, } params := make(map[string]interface{}) params["id"] = 23 sql, err := route.Sql(params, 0) if err != nil { t.Errorf("Expected not to get error, but got: %v", err) } expected := "with response_table as (select * from users where id=23) select row_to_json(t) as value from (select * from response_table) t" if sql != expected { t.Errorf("Expected sql:\n%v, but got:\n%v\n", expected, sql) } }
func checkSchema(chain *chain, value, schema interface{}) { if chain.failed() { return } valueLoader := gojsonschema.NewGoLoader(value) var schemaLoader gojsonschema.JSONLoader if str, ok := toString(schema); ok { if ok, _ := regexp.MatchString(`^\w+://`, str); ok { schemaLoader = gojsonschema.NewReferenceLoader(str) } else { schemaLoader = gojsonschema.NewStringLoader(str) } } else { schemaLoader = gojsonschema.NewGoLoader(schema) } result, err := gojsonschema.Validate(schemaLoader, valueLoader) if err != nil { chain.fail("\n%s\n\nschema:\n%s\n\nvalue:\n%s", err.Error(), dumpSchema(schema), dumpValue(value)) return } if !result.Valid() { errors := "" for _, err := range result.Errors() { errors += fmt.Sprintf(" %s\n", err) } chain.fail( "\njson schema validation failed, schema:\n%s\n\nvalue:%s\n\nerrors:\n%s", dumpSchema(schema), dumpValue(value), errors) return } }
func (a *App) LoadConfig(file string) error { f, err := os.Open(file) if err != nil { return err } defer f.Close() if err := json.NewDecoder(f).Decode(&a.Conf); err != nil { return err } a.DB, err = bolt.Open(a.DBFile, 0600, nil) if err != nil { return err } a.DB.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucketIfNotExists([]byte(PublicBucketName)) return err }) a.Router = mux.NewRouter() a.Log = logrus.New() a.Log.Out = os.Stderr a.Log.Formatter = &logrus.TextFormatter{ForceColors: true} a.Log.Level = logrus.ErrorLevel if ll, err := logrus.ParseLevel(a.LogLevel); err == nil { a.Log.Level = ll } for _, pp := range a.PublicParams { loader := gojsonschema.NewStringLoader(string(pp.RawSchema)) schema, err := gojsonschema.NewSchema(loader) if err != nil { return err } pp.Schema = schema } return nil }
// Validate uses the jsonschema to validate the configuration func Validate(config map[string]interface{}) error { schemaData, err := Asset("data/config_schema_v3.0.json") if err != nil { return err } schemaLoader := gojsonschema.NewStringLoader(string(schemaData)) dataLoader := gojsonschema.NewGoLoader(config) result, err := gojsonschema.Validate(schemaLoader, dataLoader) if err != nil { return err } if !result.Valid() { return toError(result) } return nil }
// UnmarshalAndValidateSchema attempts to validate the body against the supplied // schema, then unmarshal it into the supplied obj. func UnmarshalAndValidateSchema(body []byte, s *schema.Schema, obj interface{}) error { // Validate the body against the schema. result, err := s.Validate(schema.NewStringLoader(string(body))) if err != nil { return fail.NewBadRequestError(err) } // Return early if the json is invalid. if !result.Valid() { err := fail.NewSchemaValidationError(result.Errors()) err.Description = "It looks like one or more fields weren’t present, of the correct type, or contained an incorrect value. If it’s not obvious what the problem was, make sure you consult the API documentation. If that still doesn’t make it obvious, our documentation clearly isn’t up to scratch. Contact support and we’ll help you out." return err } // Unmarshal the body into the supplied object. err = json.Unmarshal(body, obj) if err != nil { return fail.NewBadRequestError(err) } return nil }
func (self *JSONSchemaMiddleware) LoadFromPath(base_path string) error { return filepath.Walk(base_path, func(path string, info os.FileInfo, err error) error { if err != nil { return err } if !info.Mode().IsRegular() { return nil } if !strings.HasSuffix(path, ".json") { return nil } bytes, err := ioutil.ReadFile(path) if err != nil { return fmt.Errorf("Error reading schema from %s: %s", path, err) } json_string := string(bytes) loader := gojsonschema.NewStringLoader(json_string) schema, err := gojsonschema.NewSchema(loader) if err != nil { return fmt.Errorf("Error loading schema from %s: %s", path, err) } name := filepath.Base(path) name = name[0 : len(name)-5] self.jsonSchemas[name] = &JSONSchema{ schema: schema, jsonString: json_string, } if self.logger != nil { self.logger.Debugf("Loaded schema %s", name) } return nil }) }