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

Regexps for maps #101

Closed
brownberry opened this issue Jun 14, 2017 · 3 comments
Closed

Regexps for maps #101

brownberry opened this issue Jun 14, 2017 · 3 comments

Comments

@brownberry
Copy link

feature / merge request : make regexp pattern checking available for maps with integer keys ...

modification in core.py

    # old 
    # is_present = any([re.search(required_regex, v) for v in value])
				
    # new 
    # 
    is_present = any([re.search(required_regex, str(v)) for v in value])
@martingkelly
Copy link
Contributor

Hi! I was just about to submit the same pull request, as I'm hitting the same issue as you. I think this would be an improvement. But, I realized that there is a larger issue here: kwalify seems like it can handle only string map keys, while YAML allows for any map keys. This is particularly problematic because YAML will automatically guess type, so if you ever accidentally type a key that looks like an int, then pykwalify will crash with this cryptic error:

  File "/usr/local/lib/python2.7/dist-packages/pykwalify/core.py", line 549, in _validate_mapping
    is_present = any([re.search(required_regex, v) for v in value])
  File "/usr/lib/python2.7/re.py", line 146, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or buffer

It made me wonder: Would it be possible extend the pykwalify syntax to allow map keys be first-class citizen nodes, rather than strings? In that case, they could be any type, and they could also have additional constraints put on them, such as range. In the absence of that, this patch is a good workaround.

@Grokzen, what do you think?

@martingkelly
Copy link
Contributor

martingkelly commented Aug 18, 2017

OK, looks like you can do this via a custom python extension along with the =: default map key rule. If you do func: validate_map_keys and define def validate_map_keys() as an extension, then you can check everything to your heart's content. That said, native support for this in the syntax without writing code would still be super handy, and the ability to apply regexes would also be handy.

@Grokzen
Copy link
Owner

Grokzen commented Mar 6, 2018

@brownberry @martingkelly I have to think about this because casting values to str to enable regex checks for other types feels weird in the kwalify context. To regex check a value feels like you are doing it wrong in some way.

On the idea to reworking the map keys to first-class citizens feels like a major project to work on and it would uproot a lot of stuff in the code-base. In theory sure, it could be a good idea to make working with keys easier, but on the other hand it would go away so far from the kwalify standard i am not sure i want the validation framework to go in that direction right now.

I will think about it but i will not act on it right now.

@Grokzen Grokzen closed this as completed Oct 19, 2019
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

3 participants