Skip to content

Commit

Permalink
Implement SubclassOf, rename Classy to InstanceOf
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jan 21, 2014
1 parent e6f1e23 commit 01c6001
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
22 changes: 16 additions & 6 deletions tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def test_blank_validator(self):
assert not validate(validator, int_value)[0]
assert not validate(validator, bool_value)[0]


def test_in_validator(self):
validator = {
"truthiness": [Truthy()],
Expand Down Expand Up @@ -139,12 +138,12 @@ def test_range_validator(self):
}
assert validate(validator, test_case)[0]

def test_classy_validator(self):
def test_instanceof_validator(self):
validator = {
"classy": [Required, Classy(unicode)],
"subclassy": [Required, Classy(basestring)],
"not_classy": [Required, Not(Classy(unicode))],
"not_subclassy": [Required, Not(Classy(basestring))]
"classy": [Required, InstanceOf(unicode)],
"subclassy": [Required, InstanceOf(basestring)],
"not_classy": [Required, Not(InstanceOf(unicode))],
"not_subclassy": [Required, Not(InstanceOf(basestring))]
}
test_case = {
"classy": u"unicode_string",
Expand All @@ -154,6 +153,17 @@ def test_classy_validator(self):
}
assert validate(validator, test_case)[0]

def test_subclassof_validator(self):
validator = {
"is_subclass": [Required, SubclassOf(basestring)],
"not_subclass": [Required, Not(SubclassOf(basestring))],
}
test_case = {
"is_subclass": unicode,
"not_subclass": int
}
assert validate(validator, test_case)[0]

def test_pattern_validator(self):
validator = {
"match": [Required, Pattern('\d\d\%')],
Expand Down
39 changes: 31 additions & 8 deletions validator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ def not_lambda(value):
not_lambda.err_message = "must not be blank"
elif validator.__name__ == "range_lambda":
not_lambda.err_message = "must not fall between %s and %s" % (validator.start, validator.end)
elif validator.__name__ == "class_lambda":
elif validator.__name__ == "instanceof_lambda":
not_lambda.err_message = "must not be an instance of %s or its subclasses" % validator.base_class
elif validator.__name__ == "pattern_lambda":
not_lambda.err_message = "must not match regex pattern %s" % validator.pattern
elif validator.__name__ == "subclassof_lambda":
not_lambda.err_message = "must not be a subclass of %s" % validator.base_class

return not_lambda

Expand Down Expand Up @@ -171,7 +173,7 @@ def Required(field, dictionary):

return (field in dictionary)

def Classy(base_class):
def InstanceOf(base_class):
"""
Use to specify that the
value of the key being
Expand All @@ -181,20 +183,41 @@ def Classy(base_class):
# Example:
validations = {
"field": [Classy(basestring)]
"field": [InstanceOf(basestring)]
}
passes = {"field": ""} # is a <'str'>, subclass of basestring
fails = {"field": str} # is a <'type'>
"""

def class_lambda(value):
def instanceof_lambda(value):
return isinstance(value, base_class)

class_lambda.base_class = base_class
class_lambda.err_message = "must be an instance of %s or its subclasses" % base_class.__name__
return class_lambda
instanceof_lambda.base_class = base_class
instanceof_lambda.err_message = "must be an instance of %s or its subclasses" % base_class.__name__
return instanceof_lambda

def SubclassOf(base_class):
"""
Use to specify that the
value of the key being
validated must be a subclass
of the passed in base class.
# Example:
validations = {
"field": [SubclassOf(basestring)]
}
passes = {"field": str} # is a subclass of basestring
fails = {"field": int}
"""

def subclassof_lambda(class_):
return issubclass(class_, base_class)

subclassof_lambda.base_class = base_class
subclassof_lambda.err_message = "must be a subclass of %s" % base_class.__name__
return subclassof_lambda

def Pattern(pattern):
"""
Expand Down

0 comments on commit 01c6001

Please sign in to comment.