From: Vijendar Komalla Date: Wed, 26 Jun 2013 17:39:49 +0000 (-0500) Subject: Add MinLength, MaxLength to properties schema X-Git-Tag: 2014.1~427^2 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=66fe8f10ee382686488af055ee9f202754638ce8;p=openstack-build%2Fheat-build.git Add MinLength, MaxLength to properties schema Change-Id: Ib0970e76e32f6e6d3c7f79b60c5fb3b7384fdb5d --- diff --git a/heat/engine/properties.py b/heat/engine/properties.py index d0351794..462752d9 100644 --- a/heat/engine/properties.py +++ b/heat/engine/properties.py @@ -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): diff --git a/heat/tests/test_properties.py b/heat/tests/test_properties.py index f3acff60..0d537b1a 100644 --- a/heat/tests/test_properties.py +++ b/heat/tests/test_properties.py @@ -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,