[Zope-Checkins] CVS: Zope3/lib/python/Schema - ErrorNames.py:1.1 Exceptions.py:1.3 _Field.py:1.2 _Schema.py:1.3

Klaus Wölfel woelfel@aurora-systems.de
Tue, 25 Jun 2002 10:09:56 -0400


Update of /cvs-repository/Zope3/lib/python/Schema
In directory cvs.zope.org:/tmp/cvs-serv12958

Modified Files:
	Exceptions.py _Field.py _Schema.py 
Added Files:
	ErrorNames.py 
Log Message:
Validators don't return anything any more. They only raise exceptions. ErrorNames are stored in the ErrorNames module.


=== Added File Zope3/lib/python/Schema/ErrorNames.py ===
RequiredMissing ='Must be required'
RequiredEmptyString = 'Required string is empty.'
TooBig = 'Too Big.'
TooSmall = 'Too Small.'
TooLong = 'Too Long.'
TooShort = 'Too Short.'

=== Zope3/lib/python/Schema/Exceptions.py 1.2 => 1.3 ===
 class ValidationError(Exception):
     def __init__(self,error_name):
-	Exception.__init__(self)
-	self.error_name=error_name
-	
+        Exception.__init__(self)
+        self.error_name=error_name
+
     def __cmp__(self,other):
-	return cmp(self.error_name, other.error_name)
+        return cmp(self.error_name, other.error_name)
 
 class ValidationErrorsAll(Exception):
     def __init__(self,list):
-	Exception.__init__(self)
-	self.errors = list
+        Exception.__init__(self)
+        self.errors = list
\ No newline at end of file


=== Zope3/lib/python/Schema/_Field.py 1.1 => 1.2 ===
 
 from Exceptions import StopValidation, ValidationError
+import ErrorNames
 #from _Schema import validate
 
 class Field(Attribute):
@@ -11,7 +12,7 @@
 
     default = None
     required = 0
-    
+
     def __init__(self, **kw):
         """
         Pass in field values as keyword parameters.
@@ -19,56 +20,49 @@
         for key, value in kw.items():
             setattr(self, key, value)
         super(Field, self).__init__(self.title or 'no title')
-    
+
     def validate(self, value):
         try:
-            return self._validate(value)
+            self._validate(value)
         except StopValidation:
-            return None
+            pass
 
     def _validate(self, value):
         if value is None:
             if self.required:
-                raise ValidationError, "Must be required"
+                raise ValidationError, ErrorNames.RequiredMissing
             else:
                 # we are done with validation for sure
                 raise StopValidation
-        return value
-    
+
 class Str(Field):
-    
+
     min_length = None
     max_length = None
 
     def _validate(self, value):
-        value = super(Str, self)._validate(value)
+        super(Str, self)._validate(value)
         if self.required and value == '':
-            raise ValidationError, "Required string is empty."
+            raise ValidationError,ErrorNames.RequiredEmptyString
         length = len(value)
         if self.min_length is not None and length < self.min_length:
-            raise ValidationError, "Too short."
+            raise ValidationError, ErrorNames.TooShort
         if self.max_length is not None and length >= self.max_length:
-            raise ValidationError, "Too long."
-        return value
-    
+            raise ValidationError, ErrorNames.TooLong
+
 class Bool(Field):
-        
+
     def _validate(self, value):
-        value = super(Bool, self)._validate(value)
-        return not not value
-    
+        super(Bool, self)._validate(value)
+
 class Int(Field):
 
     min = max = None
 
     def _validate(self, value):
-        value = super(Int, self)._validate(value)
+        super(Int, self)._validate(value)
         if self.min is not None and value < self.min:
-            raise ValidationError, "Too small"
-        if self.max is not None and value >= self.max:
-            raise ValidationError, "Too big"
-        return value
-    
-class DateTime(Field):
-    pass
+            raise ValidationError, ErrorNames.TooSmall
+        if self.max is not None and value > self.max:
+            raise ValidationError, ErrorNames.TooBig
 


=== Zope3/lib/python/Schema/_Schema.py 1.2 => 1.3 ===
 def validateMapping(schema, values):
     """Pass in field values in mapping and validate whether they
-    conform to schema. Stop at first error. 
+    conform to schema. Stop at first error.
     """
     from IField import IField
     for name in schema.names(1):
         attr = schema.getDescriptionFor(name)
         if IField.isImplementedBy(attr):
-	    attr.validate(values.get(name))
+            attr.validate(values.get(name))
 
 
 def validateMappingAll(schema, values):
     """Pass in field values in mapping and validate whether they
-    conform to schema. 
+    conform to schema.
     """
     list=[]
     from IField import IField
     for name in schema.names(1):
         attr = schema.getDescriptionFor(name)
         if IField.isImplementedBy(attr):
-	    try:
-		attr.validate(values.get(name))
-	    except ValidationError, e:
-		list.append((name, e))	
-    if len(list) > 0:
-	raise ValidationErrorsAll, list
-		
+            try:
+                attr.validate(values.get(name))
+            except ValidationError, e:
+                list.append((name, e))
+    if list:
+        raise ValidationErrorsAll, list
+
 # Now we can create the interesting interfaces and wire them up:
 def wire():
 
@@ -35,7 +35,7 @@
     from IField import IField
     from _Field import Field
     implements(Field, IField, 0)
-    
+
     from IField import IBool
     from _Field import Bool
     implements(Bool, IBool, 0)
@@ -48,7 +48,7 @@
     from _Field import Int
     implements(Int, IInt, 0)
 
-    
+
 wire()
-del wire 
+del wire