import ( "fmt" "github.com/go4org/jsonconfig" ) func main() { input := []byte(`{"name": "John"}`) schema := jsonconfig.Schema{ "name": jsonconfig.String(), } err := jsonconfig.ObjValidate(input, schema) if err != nil { fmt.Printf("validation failed: %s", err) } else { fmt.Println("validation succeeded") } }
import ( "fmt" "github.com/go4org/jsonconfig" ) func main() { input := []byte(` { "name": "John", "age": 30, "hobbies": [ {"name": "Reading", "level": "Intermediate"}, {"name": "Swimming", "level": "Advanced"} ], "address": {"line1": "123 Main St", "city": "Anywhere"} } `) schema := jsonconfig.Schema{ "name": jsonconfig.String(), "age": jsonconfig.Int(), "hobbies": jsonconfig.Slice(jsonconfig.Schema{ "name": jsonconfig.String(), "level": jsonconfig.String(), }), "address": jsonconfig.Schema{ "line1": jsonconfig.String(), "city": jsonconfig.String(), }, } err := jsonconfig.ObjValidate(input, schema) if err != nil { fmt.Printf("validation failed: %s", err) } else { fmt.Println("validation succeeded") } }In these examples, we are using the Obj Validate function from the go4.org.jsonconfig package to perform schema validation on JSON data. The first example validates a simple JSON object against a schema that requires a "name" property with a string value. The second example validates a more complex JSON object that includes nested objects and arrays against a schema that defines specific data types for each property.