Alberto Donato has proposed merging lp:~ack/txaws/parameter-validator into 
lp:txaws.

Requested reviews:
  txAWS Developers (txaws-dev)

For more details, see:
https://code.launchpad.net/~ack/txaws/parameter-validator/+merge/68823

This adds an optional validator argument to Parameter, to provide custom 
validation.
-- 
https://code.launchpad.net/~ack/txaws/parameter-validator/+merge/68823
Your team txAWS Developers is requested to review the proposed merge of 
lp:~ack/txaws/parameter-validator into lp:txaws.
=== modified file 'txaws/server/schema.py'
--- txaws/server/schema.py	2011-05-14 05:22:41 +0000
+++ txaws/server/schema.py	2011-07-22 11:32:36 +0000
@@ -65,16 +65,18 @@
     @param min: Minimum value for a parameter.
     @param max: Maximum value for a parameter.
     @param allow_none: Whether the parameter may be C{None}.
+    @param validator: A callable to validate the parameter, returning a bool.
     """
 
     def __init__(self, name, optional=False, default=None,
-                 min=None, max=None, allow_none=False):
+                 min=None, max=None, allow_none=False, validator=None):
         self.name = name
         self.optional = optional
         self.default = default
         self.min = min
         self.max = max
         self.allow_none = allow_none
+        self.validator = validator
 
     def coerce(self, value):
         """Coerce a single value according to this parameter's settings.
@@ -94,7 +96,10 @@
             return self.default
         self._check_range(value)
         try:
-            return self.parse(value)
+            parsed = self.parse(value)
+            if self.validator and not self.validator(parsed):
+                raise ValueError(value)
+            return parsed
         except ValueError:
             raise InvalidParameterValueError("Invalid %s value %s" %
                                              (self.kind, value))

=== modified file 'txaws/server/tests/test_schema.py'
--- txaws/server/tests/test_schema.py	2011-05-17 07:42:21 +0000
+++ txaws/server/tests/test_schema.py	2011-07-22 11:32:36 +0000
@@ -150,6 +150,27 @@
         self.assertEqual("Value (longish) for parameter Test is invalid.  "
                          "3 should be enough for anybody", error.message)
 
+    def test_validator_invalid(self):
+        """
+        L{Parameter.coerce} raises an error if the validator returns False.
+        """
+        parameter = Parameter("Test", validator=lambda _: False)
+        parameter.parse = lambda value: value
+        parameter.kind = "test_parameter"
+        error = self.assertRaises(APIError, parameter.coerce, "foo")
+        self.assertEqual(400, error.status)
+        self.assertEqual("InvalidParameterValue", error.code)
+        self.assertEqual("Invalid test_parameter value foo", error.message)
+
+    def test_validator_valid(self):
+        """
+        L{Parameter.coerce} raises an error if the validator returns False.
+        """
+        parameter = Parameter("Test", validator=lambda _: True)
+        parameter.parse = lambda value: value
+        parameter.kind = "test_parameter"
+        self.assertEqual("foo", parameter.coerce("foo"))
+
 
 class UnicodeTest(TestCase):
 

_______________________________________________
Mailing list: https://launchpad.net/~txaws-dev
Post to     : txaws-dev@lists.launchpad.net
Unsubscribe : https://launchpad.net/~txaws-dev
More help   : https://help.launchpad.net/ListHelp

Reply via email to