Validator icon indicating copy to clipboard operation
Validator copied to clipboard

Regex validator does not like pipe "|" for enum

Open rafael-bm opened this issue 3 years ago • 5 comments

Bug Description: Given a regex with that validades string that should contain either VALUEA or VALUEB

regex:^(VALUEA | VALUEB)$

Expected Behaviour: it should execute the regex

Code Fragment:

payload_schema = {
    "action": "regex:^(VALUEA|VALUEB)$"
}

payload = {
    "action": "VALUEA"
}

is_valid, _, errors = validate(payload, payload_schema, return_info=True)

Error Fragment:

/usr/local/bin/python3.9 /Users/userName/Library/Application Support/JetBrains/IntelliJIdea2022.2/scratches/scratch_51.py 
Traceback (most recent call last):
  File "/Users/userName/Library/Application Support/JetBrains/IntelliJIdea2022.2/scratches/scratch_51.py", line 25, in <module>
    v, e = validate_payload(ps, data)
  File "/Users/userName/Library/Application Support/JetBrains/IntelliJIdea2022.2/scratches/scratch_51.py", line 7, in validate_payload
    is_valid, _, errors = validate(payload, payload_schema, return_info=True)
  File "/usr/local/lib/python3.9/site-packages/validator/validator.py", line 81, in validate
    val = Validator(req, rules)
  File "/usr/local/lib/python3.9/site-packages/validator/validator.py", line 18, in __init__
    self.rules = Parser(rules).parse()
  File "/usr/local/lib/python3.9/site-packages/validator/parser/parser.py", line 26, in parse
    self.parsed_rules = {
  File "/usr/local/lib/python3.9/site-packages/validator/parser/parser.py", line 27, in <dictcomp>
    key: Translator(self.rules[key]).translate() for key in self.rules
  File "/usr/local/lib/python3.9/site-packages/validator/parser/translator.py", line 36, in translate
    rule = self._translate_str(elem)
  File "/usr/local/lib/python3.9/site-packages/validator/parser/translator.py", line 98, in _translate_str
    rule_instance = init_rule(*args)
  File "/usr/local/lib/python3.9/site-packages/validator/rules_src/regex.py", line 25, in __init__
    self.pattern = compile(pattern)
  File "/usr/local/Cellar/[email protected]/3.9.12_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/re.py", line 252, in compile
    return _compile(pattern, flags)
  File "/usr/local/Cellar/[email protected]/3.9.12_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/re.py", line 304, in _compile
    p = sre_compile.compile(pattern, flags)
  File "/usr/local/Cellar/[email protected]/3.9.12_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/sre_compile.py", line 764, in compile
    p = sre_parse.parse(p, flags)
  File "/usr/local/Cellar/[email protected]/3.9.12_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/sre_parse.py", line 950, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
  File "/usr/local/Cellar/[email protected]/3.9.12_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/sre_parse.py", line 443, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
  File "/usr/local/Cellar/[email protected]/3.9.12_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/sre_parse.py", line 838, in _parse
    raise source.error("missing ), unterminated subpattern",
re.error: missing ), unterminated subpattern at position 1

is there any other way to express an enum validation if not by regex ?

Anyone willing to contribute please review CONTRIBUTING.md for more details :100:

rafael-bm avatar Jul 27 '22 11:07 rafael-bm

Hey @rafael-bm, if you're not already working on a PR for this, I'd be willing to get something going this week

marshall7m avatar Aug 01 '22 02:08 marshall7m

Hey @rafael-bm, if you're not already working on a PR for this, I'd be willing to get something going this week

I unfortunately won't be able to work on a PR for this. Thank you for picking this bug. if u need me for testing i'm more than happy to help.

rafael-bm avatar Aug 01 '22 10:08 rafael-bm

@rafael-bm No problem, I'm glad to help especially since I had the same issue while using the Validator within my own project. That would be awesome if you can test out the PR mentioned above. Let me know if you have any question or feedback.

marshall7m avatar Aug 02 '22 06:08 marshall7m

@marshall7m my college @senemre will be checking out the branch and test it by today.

rafael-bm avatar Aug 05 '22 09:08 rafael-bm

Hey @marshall7m we could run the unit tests which you added and especially the below case exactly covers our need.

assert validate({"val": "dcdcdc"}, {"val": "required|regex:(dc|ab)*"})

so it is LGTU(s)

Thanks

senemre avatar Aug 05 '22 11:08 senemre