Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails to regex match a mapping with integer keys #167

Closed
terricain opened this issue Feb 17, 2020 · 1 comment
Closed

Fails to regex match a mapping with integer keys #167

terricain opened this issue Feb 17, 2020 · 1 comment

Comments

@terricain
Copy link

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

  1. 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

@Tanoti
Copy link

Tanoti commented Apr 20, 2020

+1 for this, we've hit the same problem

swills added a commit to swills/pykwalify that referenced this issue May 5, 2020
swills added a commit to swills/pykwalify that referenced this issue May 5, 2020
swills added a commit to swills/pykwalify that referenced this issue May 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants