[Zope3-checkins] CVS: Zope3/src/zope/schema - _field.py:1.24

Dominik Huber cvs-admin at zope.org
Mon Nov 24 12:02:15 EST 2003


Update of /cvs-repository/Zope3/src/zope/schema
In directory cvs.zope.org:/tmp/cvs-serv9487

Modified Files:
	_field.py 
Log Message:
_field.py
======
update class Object(Field):
-----------------------------------------
__init__: first set self.schema and then call the super class

_validate: add schema check

update function _validate_fields:
---------------------------------------------------
- iterate through schema.fields instead of iterating through the value.__dict__

tests/test_objectfield.py
===================
added unittest

errornames.py
===========
added  two new Errors: SchemaNotFullyImplemented, SchemaNotProvided


=== Zope3/src/zope/schema/_field.py 1.23 => 1.24 ===
--- Zope3/src/zope/schema/_field.py:1.23	Fri Nov  7 17:35:10 2003
+++ Zope3/src/zope/schema/_field.py	Mon Nov 24 12:02:14 2003
@@ -22,9 +22,11 @@
 
 from zope.interface import classImplements, implements
 from zope.interface.interfaces import IInterface
+from zope.interface.interfaces import IMethod
 
 from zope.schema.interfaces import ValidationError
 from zope.schema.errornames import WrongContainedType, WrongType
+from zope.schema import errornames
 
 from zope.schema.interfaces import IField
 from zope.schema.interfaces import IMinMaxLen, IText, ITextLine
@@ -222,36 +224,40 @@
 
 def _validate_fields(schema, value, errors=None):
     if errors is None:
-        errors = []
-
-    for name, item in value.__dict__.items():
-        field = schema[name]
-        error = None
-        try:
-            field.validate(item)
-        except ValidationError, error:
-            pass
-        else:
-            # We validated, so clear error (if any) and done with
-            # this value
-            error = None
-            break
-
-        if error is not None:
-            errors.append(error)
-
+        errors = []  
+    for name in schema.names(all=True):
+        if not IMethod.isImplementedBy(schema[name]):
+            try:
+                field = schema[name]
+                field.validate(getattr(value, name))
+            except ValidationError, error:
+                errors.append(error)
+            except AttributeError, error:
+                # property for the given name is not implemented
+                errors.append(errornames.SchemaNotFullyImplemented)
+                
     return errors
 
+
 class Object(Field):
     __doc__ = IObject.__doc__
     implements(IObject)
 
     def __init__(self, schema, **kw):
-        super(Object, self).__init__(**kw)
+        if not IInterface.isImplementedBy(schema):
+            raise ValidationError(WrongType)
+            
         self.schema = schema
-
+        super(Object, self).__init__(**kw)
+        
     def _validate(self, value):
         super(Object, self)._validate(value)
+        
+        # schema has to be implemented by value    
+        if not self.schema.isImplementedBy(value):
+            raise ValidationError(errornames.SchemaNotProvided)
+            
+        # check the value against  schema
         errors = _validate_fields(self.schema, value)
         if errors:
             raise ValidationError(WrongContainedType, errors)




More information about the Zope3-Checkins mailing list