add regex validator
This commit is contained in:
parent
49577953c6
commit
a880b1f6f9
@ -1,6 +1,7 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from functools import reduce
|
||||
from operator import or_
|
||||
import re
|
||||
|
||||
|
||||
class ValidatorException(Exception):
|
||||
@ -78,6 +79,14 @@ class NumberValidator(OrValidator):
|
||||
super().__init__(IntegerValidator(), FloatValidator())
|
||||
|
||||
|
||||
class RegexValidator(Validator):
|
||||
def __init__(self, regex: re.Pattern):
|
||||
self.regex = regex
|
||||
|
||||
def isValid(self, value):
|
||||
return self.regex.match(value) is not None
|
||||
|
||||
|
||||
validator_types = {
|
||||
"string": StringValidator,
|
||||
"str": StringValidator,
|
||||
|
13
test/property/validators/test_regex_validator.py
Normal file
13
test/property/validators/test_regex_validator.py
Normal file
@ -0,0 +1,13 @@
|
||||
from unittest import TestCase
|
||||
from owrx.property.validators import RegexValidator
|
||||
import re
|
||||
|
||||
|
||||
class RegexValidatorTest(TestCase):
|
||||
def testMatchesRegex(self):
|
||||
validator = RegexValidator(re.compile("abc"))
|
||||
self.assertTrue(validator.isValid("abc"))
|
||||
|
||||
def testDoesntMatchRegex(self):
|
||||
validator = RegexValidator(re.compile("abc"))
|
||||
self.assertFalse(validator.isValid("xyz"))
|
Loading…
Reference in New Issue
Block a user