Questions about representation of numbers
See original GitHub issueIssue Description
Dear devs, Example schema: number1:int() number2:float()
1.Why 1e4 or 1.1e4 are not recognized as valid int. Should be possible imho. 2. Why build in float validator brings error on numbers without decimal point. I.e. 10.0 is valid but 10 is not valid double. Seems illogical, since ppl never add “.” to such numbers
I have created own validator, which did the job. But may be support is natively? Thanks you very much for great tool!
`#custom type int64
class int64_type_validator(Validator):
“”" Custom Int64 validator “”"
tag = ‘int64’
def _is_valid(self, value):
strg=str(value)
try:
f=float(value)
i=np.int64(f)
return (f-i)==0
#Notes:
# TBD:May be add epsilon?
# Is 10.1e9 valid input of int64. So far yes. or do we want to limit AeB
except:
return False
class float_type_validator(Validator): “”" Custom float validator “”" tag = ‘float’
def _is_valid(self, value):
strg=str(value)
try:
f:float=float(strg);
return True
except:
return False`
Issue Analytics
- State:
- Created a year ago
- Comments:10
Yamale leaves the parsing up to the Python library being used. Yamale validates the values based on the Python types which the parser assigned. I don’t see it as a weakness of either the parser (ruamel) or the validator (yamale). Both try to be explicit in what they do. The ruamel parser assigns
1e4
to a float, and yamale correctly says a float is not an integer. For example, I’d also expect1.0
to fail a validation if an integer was required since1.0
is a float.I can see why someone would think the other way. You’re saying that if I can cast/convert a float to an integer successfully, then yamale should validate as an integer. I could also cast
1.0
as a string…but one would expect the validator to fail if the user provided1.0
and the schema expected a string instead. For that reason, yamale validates against the actual types that the parser gives it instead of what it can be cast/converted into.Both
make_data()
andmake_schema()
have aparser
parameter that you can set. There’s an example in the README here: https://github.com/23andMe/Yamale#api