validating []string{} gives invalid jsonType: []string
nachtwerk opened this issue · 2 comments
nachtwerk commented
My application uses jsonschema to validate some user provided values, one of them being a multi-select field. The incoming data is being decoded into a []string{}
but passing that as an interface returns a invalid jsonType
?
Example with schema included:
package main
import (
"fmt"
"strings"
"github.com/santhosh-tekuri/jsonschema"
)
func main() {
schema := `{"type": "array", "items": {"type": "string", "enum": ["Digital", "Radio", "TV"]}}`
value := []string{"TV"}
err := validate(schema, value)
if err != nil {
fmt.Println(err)
}
}
func validate(schemaString string, value interface{}) error {
compiler := jsonschema.NewCompiler()
if err := compiler.AddResource("obj.json", strings.NewReader(schemaString)); err != nil {
return err
}
schema, err := compiler.Compile("obj.json")
if err != nil {
return err
}
return schema.ValidateInterface(value)
}
If I am doing something wrong, please guide me to the correct docs to handle this (if the exist, I couldnt find examples)
santhosh-tekuri commented
You have to use '[]interface{}' instead of '[]string'
santhosh-tekuri commented
You can see the types allowed here
https://github.com/santhosh-tekuri/jsonschema/blob/master/schema.go#L728