add more validators
This commit is contained in:
parent
66dc4e5772
commit
4b03ced1f7
@ -1,4 +1,6 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from functools import reduce
|
||||
from operator import or_
|
||||
|
||||
|
||||
class ValidatorException(Exception):
|
||||
@ -29,19 +31,51 @@ class LambdaValidator(Validator):
|
||||
return self.callable(value)
|
||||
|
||||
|
||||
class NumberValidator(Validator):
|
||||
class TypeValidator(Validator):
|
||||
def __init__(self, type):
|
||||
self.type = type
|
||||
super().__init__()
|
||||
|
||||
def isValid(self, value):
|
||||
return isinstance(value, int) or isinstance(value, float)
|
||||
return isinstance(value, self.type)
|
||||
|
||||
|
||||
class IntegerValidator(Validator):
|
||||
class IntegerValidator(TypeValidator):
|
||||
def __init__(self):
|
||||
super().__init__(int)
|
||||
|
||||
|
||||
class FloatValidator(TypeValidator):
|
||||
def __init__(self):
|
||||
super().__init__(float)
|
||||
|
||||
|
||||
class StringValidator(TypeValidator):
|
||||
def __init__(self):
|
||||
super().__init__(str)
|
||||
|
||||
|
||||
class BoolValidator(TypeValidator):
|
||||
def __init__(self):
|
||||
super().__init__(bool)
|
||||
|
||||
|
||||
class OrValidator(Validator):
|
||||
def __init__(self, *validators):
|
||||
self.validators = validators
|
||||
super().__init__()
|
||||
|
||||
def isValid(self, value):
|
||||
return isinstance(value, int)
|
||||
return reduce(
|
||||
or_,
|
||||
[v.isValid(value) for v in self.validators],
|
||||
False
|
||||
)
|
||||
|
||||
|
||||
class StringValidator(Validator):
|
||||
def isValid(self, value):
|
||||
return isinstance(value, str)
|
||||
class NumberValidator(OrValidator):
|
||||
def __init__(self):
|
||||
super().__init__(IntegerValidator(), FloatValidator())
|
||||
|
||||
|
||||
validator_types = {
|
||||
|
17
test/property/validators/test_bool_validator.py
Normal file
17
test/property/validators/test_bool_validator.py
Normal file
@ -0,0 +1,17 @@
|
||||
from unittest import TestCase
|
||||
from owrx.property.validators import BoolValidator
|
||||
|
||||
|
||||
class NumberValidatorTest(TestCase):
|
||||
def testPassesNumbers(self):
|
||||
validator = BoolValidator()
|
||||
self.assertTrue(validator.isValid(True))
|
||||
self.assertTrue(validator.isValid(False))
|
||||
|
||||
def testDoesntPassOthers(self):
|
||||
validator = BoolValidator()
|
||||
self.assertFalse(validator.isValid(123))
|
||||
self.assertFalse(validator.isValid(-2))
|
||||
self.assertFalse(validator.isValid(.5))
|
||||
self.assertFalse(validator.isValid("text"))
|
||||
self.assertFalse(validator.isValid(object()))
|
15
test/property/validators/test_float_validator.py
Normal file
15
test/property/validators/test_float_validator.py
Normal file
@ -0,0 +1,15 @@
|
||||
from unittest import TestCase
|
||||
from owrx.property.validators import FloatValidator
|
||||
|
||||
|
||||
class FloatValidatorTest(TestCase):
|
||||
def testPassesNumbers(self):
|
||||
validator = FloatValidator()
|
||||
self.assertTrue(validator.isValid(.5))
|
||||
|
||||
def testDoesntPassOthers(self):
|
||||
validator = FloatValidator()
|
||||
self.assertFalse(validator.isValid(123))
|
||||
self.assertFalse(validator.isValid(-2))
|
||||
self.assertFalse(validator.isValid("text"))
|
||||
self.assertFalse(validator.isValid(object()))
|
17
test/property/validators/test_or_validator.py
Normal file
17
test/property/validators/test_or_validator.py
Normal file
@ -0,0 +1,17 @@
|
||||
from unittest import TestCase
|
||||
from owrx.property.validators import OrValidator, IntegerValidator, StringValidator
|
||||
|
||||
|
||||
class OrValidatorTest(TestCase):
|
||||
def testPassesAnyValidators(self):
|
||||
validator = OrValidator(IntegerValidator(), StringValidator())
|
||||
self.assertTrue(validator.isValid(42))
|
||||
self.assertTrue(validator.isValid("text"))
|
||||
|
||||
def testRejectsOtherTypes(self):
|
||||
validator = OrValidator(IntegerValidator(), StringValidator())
|
||||
self.assertFalse(validator.isValid(.5))
|
||||
|
||||
def testRejectsIfNoValidator(self):
|
||||
validator = OrValidator()
|
||||
self.assertFalse(validator.isValid("any value"))
|
Loading…
Reference in New Issue
Block a user