Commits

Mike Bayer  committed bdb0ad4

- Added public attribute ".validators" to
Mapper, an immutable dictionary view of
all attributes that have been decorated
with the @validates decorator.
[ticket:2240] courtesy Stefano Fontanelli

  • Participants
  • Parent commits 87a1dc5

Comments (0)

Files changed (4)

     result set.  [ticket:2215]
     Also in 0.6.9.
 
+  - Added public attribute ".validators" to 
+    Mapper, an immutable dictionary view of 
+    all attributes that have been decorated
+    with the @validates decorator.
+    [ticket:2240] courtesy Stefano Fontanelli
+
   - Fixed subtle bug that caused SQL to blow
     up if: column_property() against subquery +
     joinedload + LIMIT + order by the column

File lib/sqlalchemy/orm/mapper.py

                                         polymorphic_on, 
                                         "polymorphic_on")
         self._dependency_processors = []
-        self._validators = {}
+        self.validators = util.immutabledict()
         self.passive_updates = passive_updates
         self._clause_adapter = None
         self._requires_row_aliasing = False
     
     """
 
+    validators = None
+    """An immutable dictionary of attributes which have been decorated
+    using the :func:`~.orm.validates` decorator.    
+    
+    The dictionary contains string attribute names as keys
+    mapped to the actual validation method.
+    
+    """
+
     c = None
     """A synonym for :attr:`~.Mapper.columns`."""
 
                     event.listen(manager, 'load', _event_on_load, raw=True)
                 elif hasattr(method, '__sa_validators__'):
                     for name in method.__sa_validators__:
-                        self._validators[name] = method
+                        self.validators = self.validators.union(
+                            {name : method}
+                        )
 
         manager.info[_INSTRUMENTOR] = self
 

File lib/sqlalchemy/orm/strategies.py

     if useobject and prop.single_parent:
         listen_hooks.append(single_parent_validator)
 
-    if prop.key in prop.parent._validators:
+    if prop.key in prop.parent.validators:
         listen_hooks.append(
             lambda desc, prop: mapperutil._validator_events(desc, 
                                 prop.key, 
-                                prop.parent._validators[prop.key])
+                                prop.parent.validators[prop.key])
         )
 
     if useobject:

File test/orm/test_mapper.py

         sess.expunge_all()
         eq_(sess.query(User).filter_by(name='ed modified').one(), User(name='ed'))
 
-
     def test_collection(self):
         users, addresses, Address = (self.tables.users,
                                 self.tables.addresses,
             User(name='edward', addresses=[Address(email_address='foo@bar.com')])
         )
 
+    def test_validators_dict(self):
+        users, addresses, Address = (self.tables.users,
+                                     self.tables.addresses,
+                                     self.classes.Address)
+
+        class User(fixtures.ComparableEntity):
+
+            @validates('name')
+            def validate_name(self, key, name):
+                assert name != 'fred'
+                return name + ' modified'
+
+            @validates('addresses')
+            def validate_address(self, key, ad):
+                assert '@' in ad.email_address
+                return ad
+
+            def simple_function(self, key, value):
+                return key, value
+
+        u_m = mapper(User,
+                      users,
+                      properties={'addresses':relationship(Address)})
+        mapper(Address, addresses)
+
+        eq_(
+            dict((k, v.__name__) for k, v in u_m.validators.items()),
+            {'name':'validate_name', 
+            'addresses':'validate_address'}
+        )
+
 class ComparatorFactoryTest(_fixtures.FixtureTest, AssertsCompiledSQL):
     def test_kwarg_accepted(self):
         users, Address = self.tables.users, self.classes.Address