Skip to content

Commit 34aa9c1

Browse files
add new validator
1 parent c2337c1 commit 34aa9c1

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

openhtf/util/validators.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,36 @@ def __ne__(self, other) -> bool:
440440
def matches_regex(regex):
441441
return RegexMatcher(regex, re.compile(regex))
442442

443+
class MultiRegexMatcher(ValidatorBase):
444+
def __init__(self, regex_list: list[str], compiled_list: list[re.Pattern]) -> None:
445+
self.regex_list = regex_list
446+
self._compiled_list = compiled_list
447+
448+
def __call__(self, value: str) -> bool:
449+
str_value = str(value)
450+
for compiled_pattern in self._compiled_list:
451+
if compiled_pattern.match(str_value) is not None:
452+
return True
453+
return False
454+
455+
def __deepcopy__(self, dummy_memo):
456+
return type(self)(self.regex_list[:], self._compiled_list[:])
457+
458+
def __str__(self):
459+
patterns_str = " | ".join(self.regex_list)
460+
return "'x' matches any of: /%s/" % patterns_str
461+
462+
def __eq__(self, other):
463+
return isinstance(other, type(self)) and self.regex_list == other.regex_list
464+
465+
def __ne__(self, other) -> bool:
466+
return not self == other
467+
468+
@register
469+
def matches_any_regex(regex_list: list[str]):
470+
compiled_list = [re.compile(regex) for regex in regex_list]
471+
return MultiRegexMatcher(regex_list, compiled_list)
472+
443473

444474
class WithinPercent(RangeValidatorBase):
445475
"""Validates that a number is within percent of a value."""

0 commit comments

Comments
 (0)