CSenshi / Validator

Easy-to-use, Highly Configurable Python Data Validator. Inspired by Laravel Validator
https://pypi.org/project/validator/
MIT License
46 stars 23 forks source link

An integer expected, but the text can be a valid value. #126

Open dzhaugasharov opened 3 years ago

dzhaugasharov commented 3 years ago

Bug Description: This rule is valid? An integer expected, but the text can be a valid value.

Code Fragment:

valid = Validator({'session_id': 'abcd'}, {'session_id': 'required|integer|min:1'})
karolyjozsa commented 2 years ago

Validating 'aaa' as an integer is pretty confusing.

>>> validate({"d":'aaa'}, {"d":'integer|min:1'}, return_info=True)
(True, {'d': 'aaa'}, {})

I think the root issue is that the request is somehow converted to an integer, which has not much to do with the original string. In this example the error message indicates this conversion: 'aaa' -> 2730

>>> validate({"d":'aaa'}, {"d":'integer|max:1'}, return_info=True)
(False, {}, {'d': {'Max': 'Expected Maximum: 1, Got: 2730'}})
Sierra77 commented 1 month ago

This i caused by the function Hex.convert in this line: https://github.com/CSenshi/Validator/blob/master/validator/rules_src/integer.py#L46. The function converts the hex chars ("abcdef") to integers. In fact hex:aaa = dec:2730. I know that is not a solution but just a workaround, but if you don't need the hex check you can remove that function