voxpupuli / json-schema

Ruby JSON Schema Validator
MIT License
1.52k stars 241 forks source link

Type defined as string gets rejects a string integer #475

Open andykoch opened 1 year ago

andykoch commented 1 year ago

I would expect a string that is just a number to pass validation as a string.

 JSON::Validator.fully_validate({type: "string"}, "123")
=> ["The property '#/' of type integer did not match the following type: string in schema"]
 JSON::Validator.fully_validate({type: "string"}, "123-Foo")
=> []

When I use the same schema and value in a third party validator, it passes fine.

According to the json schema specs: https://json-schema.org/understanding-json-schema/reference/string.html#string

image
andykoch commented 1 year ago

I dug into the source and discovered the string was getting JSON.parse() called on it, which returns integer 123. To address that I appended a to_json.

JSON::Validator.fully_validate({type: "string"}, "123".to_json)

"123".to_json returns "\"123\""