santhosh-tekuri/jsonschema

JSON Extensions not working?

Closed this issue · 1 comments

I just found your library and am trying to get a keyword extension working. I tried running the example provided in the docs: https://pkg.go.dev/github.com/santhosh-tekuri/jsonschema/v5#example-package-Extension

It doesn't look like it is working as intended. The Validate method always seems to call the default case... resulting in no validation being performed. The culprit is v is of type *strings.Reader when it should be a number.

I don't have any experience using this library so I am not sure if its a bug or the example is faulty.

func (s powerOfSchema) Validate(ctx jsonschema.ValidationContext, v interface{}) error {
	switch v.(type) {
	case json.Number, float64, int, int32, int64:
		fmt.Println("YES")
		pow := int64(s)
		n, _ := strconv.ParseInt(fmt.Sprint(v), 10, 64)
		for n%pow == 0 {
			n = n / pow
		}
		if n != 1 {
			return ctx.Error("powerOf", "%v not powerOf %v", v, pow)
		}
		return nil
	default:
		fmt.Println("switch.DEFAULT")
		return nil
	}
}

thanks for reporting the bug. the bug is in the example. i fixed it with 2ee8058.

the issue is. sch.Validate takes the parsed json object. but in the example_extension_test.go i was passing io.Reader to validate.