]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Add MinLength, MaxLength to properties schema
authorVijendar Komalla <vijendar.komalla@RACKSPACE.COM>
Wed, 26 Jun 2013 17:39:49 +0000 (12:39 -0500)
committerGerrit Code Review <review@openstack.org>
Wed, 26 Jun 2013 21:06:10 +0000 (21:06 +0000)
Change-Id: Ib0970e76e32f6e6d3c7f79b60c5fb3b7384fdb5d

heat/engine/properties.py
heat/tests/test_properties.py

index d0351794ed8c055bac60243ec29dab703d861d87..462752d93942c9f768e67b34dc79f6752ccd6994 100644 (file)
@@ -21,10 +21,11 @@ from heat.common import exception
 
 SCHEMA_KEYS = (
     REQUIRED, IMPLEMENTED, DEFAULT, TYPE, SCHEMA,
-    PATTERN, MIN_VALUE, MAX_VALUE, VALUES,
+    PATTERN, MIN_VALUE, MAX_VALUE, VALUES, MIN_LENGTH, MAX_LENGTH,
 ) = (
     'Required', 'Implemented', 'Default', 'Type', 'Schema',
     'AllowedPattern', 'MinValue', 'MaxValue', 'AllowedValues',
+    'MinLength', 'MaxLength',
 )
 
 SCHEMA_TYPES = (
@@ -104,6 +105,17 @@ class Property(object):
                 raise ValueError('"%s" does not match pattern "%s"' %
                                  (value, pattern))
 
+        if MIN_LENGTH in self.schema:
+            min_length = int(self.schema[MIN_LENGTH])
+            if len(value) < min_length:
+                raise ValueError('Minimum string length is %d characters.' %
+                                 min_length)
+
+        if MAX_LENGTH in self.schema:
+            max_length = int(self.schema[MAX_LENGTH])
+            if len(value) > max_length:
+                raise ValueError('Maximum string length is %d characters.' %
+                                 max_length)
         return value
 
     def _validate_map(self, value):
index f3acff60e03598154db78eafd42f653da9fd2048..0d537b1aa4e023d5c20e19b9e7e3edc59c1688d8 100644 (file)
@@ -95,6 +95,37 @@ class PropertyTest(testtools.TestCase):
         p = properties.Property(schema)
         self.assertRaises(ValueError, p.validate_data, 'blarg')
 
+    def test_string_maxlength_good(self):
+        schema = {'Type': 'String',
+                  'MaxLength': '5'}
+        p = properties.Property(schema)
+        self.assertEqual(p.validate_data('abcd'), 'abcd')
+
+    def test_string_exceeded_maxlength(self):
+        schema = {'Type': 'String',
+                  'MaxLength': '5'}
+        p = properties.Property(schema)
+        self.assertRaises(ValueError, p.validate_data, 'abcdef')
+
+    def test_string_length_in_range(self):
+        schema = {'Type': 'String',
+                  'MinLength': '5',
+                  'MaxLength': '10'}
+        p = properties.Property(schema)
+        self.assertEqual(p.validate_data('abcdef'), 'abcdef')
+
+    def test_string_minlength_good(self):
+        schema = {'Type': 'String',
+                  'MinLength': '5'}
+        p = properties.Property(schema)
+        self.assertEqual(p.validate_data('abcde'), 'abcde')
+
+    def test_string_smaller_than_minlength(self):
+        schema = {'Type': 'String',
+                  'MinLength': '5'}
+        p = properties.Property(schema)
+        self.assertRaises(ValueError, p.validate_data, 'abcd')
+
     def test_int_good(self):
         schema = {'Type': 'Integer',
                   'MinValue': 3,