Questions about representation of numbersSee original GitHub issue
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
“”" 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`
- Created 2 years ago
Top GitHub Comments
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 expect
1.0 to fail a validation if an integer was required since
1.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 provided
1.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.
make_schema() have a
parser parameter that you can set. There’s an example in the README here: https://github.com/23andMe/Yamale#api
# Import Yamale and make a schema object, make sure ruamel.yaml is installed already. import yamale schema = yamale.make_schema('./schema.yaml', parser='ruamel') # Create a Data object data = yamale.make_data('./data.yaml', parser='ruamel') # Validate data against the schema same as before. yamale.validate(schema, data)