qri-io / jsonschema

golang implementation of https://json-schema.org drafts 7 & 2019-09
MIT License
461 stars 54 forks source link

feat(type): support additional number types #72

Closed saracen closed 4 years ago

saracen commented 4 years ago

At present, only Go float64 types are supported. Validating an integer in an interface{} of an unsupported number type currently displays an "unknown" type error.

This adds support for additional Go numeric types: uint, ints and float32s and converts them to float64's where comparisons are performed.

This is useful for cases where the interface was marshalled with an alternative json library, or in my case a yaml library, that uses ints instead of floats where it can. This also allows jsonschema to be used more generically against other interfaces.

saracen commented 4 years ago

Moving this to draft status, as I realised what I've added so far only solves one half of the problem.

saracen commented 4 years ago

This is hopefully now ready for review.

I changed %f to %v for printing the float values in the error reports. Although the Go type is always a float, the error message can be confusing in instances where the schema requires an integer but the error returns something like: 5.0 must be less than 4.0.

Arqu commented 4 years ago

Brilliant, thank you.