[Zope-Checkins] CVS: Zope3/lib/python/Schema - IField.py:1.4 IValidator.py:1.2 Validator.py:1.3 _Field.py:1.6 _Schema.py:1.6
Martijn Faassen
m.faassen@vet.uu.nl
Sun, 14 Jul 2002 14:51:28 -0400
Update of /cvs-repository/Zope3/lib/python/Schema
In directory cvs.zope.org:/tmp/cvs-serv4517
Modified Files:
IField.py IValidator.py Validator.py _Field.py _Schema.py
Log Message:
During the Zope3 sprint in Charleroi, it was decided that the contract
of the validate() method was not to ever convert. It would raise an
exception if validation fails, and otherwise pass silently. We
refactored the code accordingly. This was inadvertently changed by
Stephan back to the original behavior (to assume conversion) in his
recent checkins when he "finished the Schema code".
Unfortunately I do not think we stated this explicitly in the
interface, though checking CVS entry messages or talking to the people
who last worked on the schema would've spared Stephan and now me a lot
of useless work. :(
Whitespace normalization, the only validator which actually seemed to
be doing any conversion (changing its input), belongs in the converter
code, not in the validator code. Where the converter code belongs is
another debate. My opinion is that while which conversions to use is
part of the particular view, many common conversions also make sense to
be part of the schema package.
=== Zope3/lib/python/Schema/IField.py 1.3 => 1.4 ===
description="Default.",
default="")
- whitespace = Field.Str(
- title="Whitespace",
- description="preserve: whitespace is preserved."
- "replace: all occurences of tab, line feed and "
- "carriage return are replaced with space characters. "
- "collapse: first process as in 'replace', then "
- "collapse all spaces to a single space, and strip any "
- "spaces from front and back."
- "strip: strip off whitespace from front and back.",
- allowed_values=("preserve", "replace", "collapse", "strip"),
- default="strip")
-
min_length = Field.Int(
title="Minimum length",
description=("Value after whitespace processing cannot have less than "
=== Zope3/lib/python/Schema/IValidator.py 1.1 => 1.2 ===
from Interface import Interface
class IValidator(Interface):
- """It's repsonsibility lies in validating a particular value against the
- specifed field. Each Validator just does one check, like check for the
- max value or the min value!
+ """Validates a particular value against the specifed field. Each
+ Validator just does one check, like check for the max value or the
+ min value!
+ Note that the responsibility of Validator is not to change the
+ value, only to raise an exception in case the value was incorrect.
+ Converters are used to change values.
+
It should be always implemented as an adapter.
"""
def validate(value):
- """Validate the the value. Note that this method must always
- return the value."""
+ """Validate the the value.
+
+ This should not return anything, only raise an exception in case
+ of an invalid value."""
=== Zope3/lib/python/Schema/Validator.py 1.2 => 1.3 ===
def validate(self, value):
'See Schema.IValidator.IValidator'
- return value
+ pass
class TypeValidator(Validator):
@@ -45,7 +45,6 @@
t = self.field.type
if t is not None and value is not None and not isinstance(value, t):
raise ValidationError, ErrorNames.WrongType
- return value
class RequiredValidator(Validator):
"""If no value was passed, check whether the field was required."""
@@ -57,7 +56,6 @@
else:
# we are done with validation for sure
raise StopValidation
- return value
class StrRequiredValidator(Validator):
"""Empty Str are not acceptable for a required field."""
@@ -65,7 +63,6 @@
'See Schema.IValidator.IValidator'
if self.field.required and value == '':
raise ValidationError, ErrorNames.RequiredEmptyStr
- return value
class MinimumLengthValidator(Validator):
"""Check that the length is larger than the minimum value."""
@@ -75,7 +72,6 @@
if self.field.min_length is not None and \
length < self.field.min_length:
raise ValidationError, ErrorNames.TooShort
- return value
class MaximumLengthValidator(Validator):
"""Check that the length is smaller than the maximum value."""
@@ -85,7 +81,6 @@
if self.field.max_length is not None and \
length > self.field.max_length:
raise ValidationError, ErrorNames.TooLong
- return value
class MinimumValueValidator(Validator):
"""Check that the value is larger than the minimum value."""
@@ -93,7 +88,6 @@
'See Schema.IValidator.IValidator'
if self.field.min is not None and value < self.field.min:
raise ValidationError, ErrorNames.TooSmall
- return value
class MaximumValueValidator(Validator):
"""Check that the value is smaller than the maximum value."""
@@ -101,7 +95,6 @@
'See Schema.IValidator.IValidator'
if self.field.max is not None and value > self.field.max:
raise ValidationError, ErrorNames.TooBig
- return value
class AllowedValuesValidator(Validator):
"""Check whether the value is in one of the allowed values."""
@@ -110,22 +103,6 @@
allowed = self.field.allowed_values
if len(allowed) > 0 and value not in allowed:
raise ValidationError, ErrorNames.InvalidValue
- return value
-
-class WhitespaceValidator(Validator):
- """Check and convert the whitespaces of the value."""
- def validate(self, value):
- 'See Schema.IValidator.IValidator'
- option = self.field.whitespaces
- if option in ('replace', 'collapse'):
- value = value.replace('\t', ' ')
- value = value.replace('\r', ' ')
- value = value.replace('\n', ' ')
- if option == 'collapse':
- value = ' '.join(value.split())
- if option == 'strip':
- value = value.strip()
- return value
class DecimalsValidator(Validator):
"""Check that the float value has the right precision."""
@@ -138,7 +115,6 @@
decimals = 0
if self.field.decimals and decimals > self.field.decimals:
raise ValidationError, ErrorNames.TooManyDecimals
- return value
def _flatten(list):
out = []
@@ -159,7 +135,6 @@
for val in value:
if not isinstance(val, types):
raise ValidationError, ErrorNames.WrongContainedType
- return value
class MinimumAmountOfItemsValidator(Validator):
"""Check whether the list contains enough items."""
@@ -168,7 +143,6 @@
if self.field.min_values and value is not None and \
len(value) < self.field.min_values:
raise ValidationError, ErrorNames.NotEnoughElements
- return value
class MaximumAmountOfItemsValidator(Validator):
"""Check whether the list contains not too many items."""
@@ -176,7 +150,6 @@
'See Schema.IValidator.IValidator'
if self.field.max_values and len(value) > self.field.max_values:
raise ValidationError, ErrorNames.TooManyElements
- return value
class DictKeyTypeValidator(Validator):
"""Check that the values in the value have the right type."""
@@ -187,7 +160,6 @@
value_types=self.field.key_types)
validator = ListValueTypeValidator(field)
validator.validate(keys)
- return value
class DictValueTypeValidator(Validator):
"""Check that the values in the value have the right type."""
@@ -198,7 +170,6 @@
value_types=self.field.value_types)
validator = ListValueTypeValidator(field)
validator.validate(values)
- return value
class ContainerValidator(Validator):
""" """
@@ -208,10 +179,9 @@
'See Schema.IValidator.IValidator'
for validator in self.validators:
try:
- value = validator(self.field).validate(value)
+ validator(self.field).validate(value)
except StopValidation:
- return value
- return value
+ return
class SingleValueValidator(ContainerValidator):
"""Validator for Single Value Fields"""
@@ -220,7 +190,6 @@
class StrValidator(SingleValueValidator):
"""Completely validates a Str Field."""
validators = SingleValueValidator.validators + [StrRequiredValidator,
- WhitespaceValidator,
MinimumLengthValidator,
MaximumLengthValidator]
=== Zope3/lib/python/Schema/_Field.py 1.5 => 1.6 ===
type = str
min_length = None
max_length = None
- whitespaces = "preserve"
class Bool(SingleValueField):
"""A field representing a Bool."""
=== Zope3/lib/python/Schema/_Schema.py 1.5 => 1.6 ===
attr = schema.getDescriptionFor(name)
if IField.isImplementedBy(attr):
attr.validate(values.get(name))
- return 1
def validateMappingAll(schema, values):
"""Pass in field values in mapping and validate whether they
@@ -51,7 +50,6 @@
list.append((name, e))
if list:
raise ValidationErrorsAll, list
- return 1
# Now we can create the interesting interfaces and wire them up:
def wire():