CSenshi / Validator

Easy-to-use, Highly Configurable Python Data Validator. Inspired by Laravel Validator
https://pypi.org/project/validator/
MIT License
46 stars 23 forks source link

Regex validator does not like pipe "|" for enum #131

Closed rafael-bm closed 1 month ago

rafael-bm commented 2 years ago

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/python@3.9/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/python@3.9/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/python@3.9/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/python@3.9/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/python@3.9/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/python@3.9/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:

marshall7m commented 2 years ago

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

rafael-bm commented 2 years ago

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.

marshall7m commented 2 years ago

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

rafael-bm commented 2 years ago

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

senemre commented 2 years ago

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