keleshev / schema

Schema validation just got Pythonic
MIT License
2.87k stars 214 forks source link

Float is not validated according to specification (float is not supersetting int) #249

Closed hnkelau closed 3 years ago

hnkelau commented 3 years ago

Hello!

I ran into a failed validation today and as far as I can tell float validation is not following JSON Schema convention when validating a float.

In [8]: Schema({'float':float}).validate({'float':1})

raises:

SchemaError: Key 'float' error: 1 should be instance of 'float

when it should pass the validation according to https://json-schema.org/understanding-json-schema/reference/numeric.html#number given the following correctly compiled schema:

In [16]: Schema({'float':float}).json_schema(1)
Out[16]:
{'type': 'object',
'properties': {'float': {'type': 'number'}},
'required': ['float'], 'additionalProperties': False, '$id': 1, '$schema': 'http://json-schema.org/draft-07/schema#'}

It looks like the "double assert" at https://github.com/keleshev/schema/blob/master/test_schema.py#L249 is hiding this issue.

wangxingyao commented 3 years ago

Schema({'float': float}).validate({'float':1.0})

or

from schema import Use Schema({'float': Use(float)}).validate({'float':1})

hnkelau commented 3 years ago

Thanks for the suggested solutions!

from schema import Use Schema({'float': Use(float)}).validate({'float':1})

Will work wonders!