Fails to regex match a mapping with integer keys
terricain opened this issue · comments
Environment
- Python version: 3.8.0
- PyKwalify version: 1.7.0
Tried to validate a mapping key with a regex but the mapping key is an integer.
If there is a better way to do this let me know. I think you should either quote the keys, or as you are trying to regex them, stringify them before regexing
Steps to Reproduce
- pykwalify -s schema.yaml -d data.yaml
Schema
type: map
mapping:
test:
type: map
mapping:
regex;(.+):
type: str
Data
test:
1: test
2: test2
Expected Behavior
Validation passed
Observed Behavior
Got exception
Traceback (most recent call last):
File "/home/terry/.virtualenv/ecs_v2/bin/pykwalify", line 10, in <module>
sys.exit(cli_entrypoint())
File "/home/terry/.virtualenv/ecs_v2/lib/python3.8/site-packages/pykwalify/cli.py", line 95, in cli_entrypoint
run(parse_cli())
File "/home/terry/.virtualenv/ecs_v2/lib/python3.8/site-packages/pykwalify/cli.py", line 82, in run
c.validate()
File "/home/terry/.virtualenv/ecs_v2/lib/python3.8/site-packages/pykwalify/core.py", line 155, in validate
self._start_validate(self.source)
File "/home/terry/.virtualenv/ecs_v2/lib/python3.8/site-packages/pykwalify/core.py", line 202, in _start_validate
self._validate(value, root_rule, path, done)
File "/home/terry/.virtualenv/ecs_v2/lib/python3.8/site-packages/pykwalify/core.py", line 236, in _validate
self._validate_mapping(value, rule, path, done=None)
File "/home/terry/.virtualenv/ecs_v2/lib/python3.8/site-packages/pykwalify/core.py", line 570, in _validate_mapping
self._validate(v, r, u"{0}/{1}".format(path, k), done)
File "/home/terry/.virtualenv/ecs_v2/lib/python3.8/site-packages/pykwalify/core.py", line 236, in _validate
self._validate_mapping(value, rule, path, done=None)
File "/home/terry/.virtualenv/ecs_v2/lib/python3.8/site-packages/pykwalify/core.py", line 544, in _validate_mapping
is_present = any([re.search(required_regex, v) for v in value])
File "/home/terry/.virtualenv/ecs_v2/lib/python3.8/site-packages/pykwalify/core.py", line 544, in <listcomp>
is_present = any([re.search(required_regex, v) for v in value])
File "/usr/local/lib/python3.8/re.py", line 199, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object
Is because the list comprehension here https://github.com/Grokzen/pykwalify/blob/master/pykwalify/core.py#L578 - is trying to run a regex search on a non-string value.
Could be fixed by stringifying the value inside the list comp
is_present = any([re.search(required_regex, str(v)) for v in value])
I can make a PR if your happy with this solution
+1 for this, we've hit the same problem