[Zope-Checkins] CVS: Zope3/lib/python/Schema - Validator.py:1.4 _Field.py:1.7
Martijn Faassen
m.faassen@vet.uu.nl
Sun, 14 Jul 2002 16:00:57 -0400
Update of /cvs-repository/Zope3/lib/python/Schema
In directory cvs.zope.org:/tmp/cvs-serv23062
Modified Files:
Validator.py _Field.py
Log Message:
Beginnings of a refactoring of Validator. Mostly renaming and some
changes to the hierarchy. Actually I hope the hierarchy inside Validator
can mostly go away in favor of one of Fields, later (maintaining two
related hierarchies is a pain).
Also some bugfixes (missing ValidationError import) to the tests. Though
now I'm wondering whether we need a try.. except ValidationError in the
tests at all; it seems to obscure the tests more than they help, so removing
the 'try' and 'except' and simply letting the block of code unprotected
may be better -- any unexpected ValidationError will cause the test to
fail with an error.
=== Zope3/lib/python/Schema/Validator.py 1.3 => 1.4 ===
raise StopValidation
class StrRequiredValidator(Validator):
- """Empty Str are not acceptable for a required field."""
+ """Don't accept empty strings for a required field."""
def validate(self, value):
'See Schema.IValidator.IValidator'
if self.field.required and value == '':
raise ValidationError, ErrorNames.RequiredEmptyStr
class MinimumLengthValidator(Validator):
- """Check that the length is larger than the minimum value."""
+ """Check that the length is larger than the minimum."""
def validate(self, value):
'See Schema.IValidator.IValidator'
length = len(value)
@@ -74,7 +74,7 @@
raise ValidationError, ErrorNames.TooShort
class MaximumLengthValidator(Validator):
- """Check that the length is smaller than the maximum value."""
+ """Check that the length is smaller than the maximum."""
def validate(self, value):
'See Schema.IValidator.IValidator'
length = len(value)
@@ -83,21 +83,21 @@
raise ValidationError, ErrorNames.TooLong
class MinimumValueValidator(Validator):
- """Check that the value is larger than the minimum value."""
+ """Check that the value is larger than the minimum."""
def validate(self, value):
'See Schema.IValidator.IValidator'
if self.field.min is not None and value < self.field.min:
raise ValidationError, ErrorNames.TooSmall
class MaximumValueValidator(Validator):
- """Check that the value is smaller than the maximum value."""
+ """Check that the value is smaller than the maximum."""
def validate(self, value):
'See Schema.IValidator.IValidator'
if self.field.max is not None and value > self.field.max:
raise ValidationError, ErrorNames.TooBig
class AllowedValuesValidator(Validator):
- """Check whether the value is in one of the allowed values."""
+ """Check whether the value is one of the allowed values."""
def validate(self, value):
'See Schema.IValidator.IValidator'
allowed = self.field.allowed_values
@@ -126,7 +126,7 @@
return out
class ListValueTypeValidator(Validator):
- """Check that the values in the value have the right type."""
+ """Check that all list elements have the right type."""
def validate(self, value):
'See Schema.IValidator.IValidator'
if self.field.value_types:
@@ -171,9 +171,9 @@
validator = ListValueTypeValidator(field)
validator.validate(values)
-class ContainerValidator(Validator):
- """ """
- validators = [TypeValidator, RequiredValidator]
+class CombinedValidator(Validator):
+ """Combine several validators into a larger one."""
+ validators = []
def validate(self, value):
'See Schema.IValidator.IValidator'
@@ -183,33 +183,42 @@
except StopValidation:
return
-class SingleValueValidator(ContainerValidator):
- """Validator for Single Value Fields"""
- validators = ContainerValidator.validators + [AllowedValuesValidator]
-
-class StrValidator(SingleValueValidator):
+class BaseValidator(CombinedValidator):
+ """A simple base validator."""
+ validators = [TypeValidator, RequiredValidator, AllowedValuesValidator]
+
+class SimpleValidator(BaseValidator):
+ """Validator for values of simple type.
+ A simple type cannot be further decomposed into other types."""
+
+class StrValidator(SimpleValidator):
"""Completely validates a Str Field."""
- validators = SingleValueValidator.validators + [StrRequiredValidator,
- MinimumLengthValidator,
- MaximumLengthValidator]
+ validators = SimpleValidator.validators + [StrRequiredValidator,
+ MinimumLengthValidator,
+ MaximumLengthValidator]
-class BoolValidator(SingleValueValidator):
+class BoolValidator(SimpleValidator):
"""Completely validates a Bool Field."""
-class IntValidator(SingleValueValidator):
+class IntValidator(SimpleValidator):
"""Completely validates a Int Field."""
- validators = SingleValueValidator.validators + [MinimumValueValidator,
- MaximumValueValidator]
+ validators = SimpleValidator.validators + [MinimumValueValidator,
+ MaximumValueValidator]
-class FloatValidator(SingleValueValidator):
+class FloatValidator(SimpleValidator):
"""Completely validates a Float Field."""
- validators = SingleValueValidator.validators + [MinimumValueValidator,
- MaximumValueValidator,
- DecimalsValidator]
+ validators = SimpleValidator.validators + [MinimumValueValidator,
+ MaximumValueValidator,
+ DecimalsValidator]
+
+class CompositeValidator(BaseValidator):
+ """Validator for values of composite type.
+ A a composite type one composed of several others, i.e. an
+ object but also a list, tuple or mapping."""
-class MultiValueValidator(ContainerValidator):
+class MultiValueValidator(BaseValidator):
"""Validator for Single Value Fields"""
- validators = ContainerValidator.validators + [
+ validators = BaseValidator.validators + [
ListValueTypeValidator,
MinimumAmountOfItemsValidator, MaximumAmountOfItemsValidator]
@@ -219,9 +228,9 @@
class ListValidator(TupleValidator):
"""Completely validates a List Field."""
-class DictValidator(MultiValueValidator):
+class DictValidator(CompositeValidator):
"""Completely validates a Dict Field."""
- validators = ContainerValidator.validators + [
+ validators = CompositeValidator.validators + [
DictKeyTypeValidator, DictValueTypeValidator,
MinimumAmountOfItemsValidator, MaximumAmountOfItemsValidator]
=== Zope3/lib/python/Schema/_Field.py 1.6 => 1.7 ===
type = None
default = None
required = 0
-
+ allowed_values = []
+
def __init__(self, **kw):
"""Pass in field values as keyword parameters."""
for key, value in kw.items():
@@ -40,26 +41,22 @@
except StopValidation:
return value
-class SingleValueField(Field):
- """A field that contains only one value."""
- allowed_values = []
-
-class Str(SingleValueField):
+class Str(Field):
"""A field representing a Str."""
type = str
min_length = None
max_length = None
-class Bool(SingleValueField):
+class Bool(Field):
"""A field representing a Bool."""
type = type(not 1)
-class Int(SingleValueField):
+class Int(Field):
"""A field representing a Integer."""
type = int
min = max = None
-class Float(SingleValueField):
+class Float(Field):
"""A field representing a Floating Point."""
type = float, int
min = max = None