raise ValueError('"%s" does not match pattern "%s"' %
(value, pattern))
+ self._validate_min_max_length(value, STRING)
+ return value
+
+ def _validate_min_max_length(self, value, value_type):
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)
+ raise ValueError('Minimum %s length is %d' %
+ (value_type, 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
+ raise ValueError('Maximum %s length is %d' %
+ (value_type, max_length))
def _validate_map(self, value):
if value is None:
else:
children = value
+ self._validate_min_max_length(value, MAP)
return children
def _validate_list(self, value):
else:
children = value
+ self._validate_min_max_length(value, LIST)
return children
def _validate_bool(self, value):
p = properties.Property(schema)
self.assertEqual(p.validate_data(['bar', 'foo']), ['bar', 'foo'])
+ def test_list_maxlength_good(self):
+ schema = {'Type': 'List',
+ 'MaxLength': '3'}
+ p = properties.Property(schema)
+ self.assertEqual(p.validate_data(['1', '2']), ['1', '2'])
+
+ def test_list_exceeded_maxlength(self):
+ schema = {'Type': 'List',
+ 'MaxLength': '2'}
+ p = properties.Property(schema)
+ self.assertRaises(ValueError, p.validate_data, ['1', '2', '3'])
+
+ def test_list_length_in_range(self):
+ schema = {'Type': 'List',
+ 'MinLength': '2',
+ 'MaxLength': '4'}
+ p = properties.Property(schema)
+ self.assertEqual(p.validate_data(['1', '2', '3']), ['1', '2', '3'])
+
+ def test_list_minlength_good(self):
+ schema = {'Type': 'List',
+ 'MinLength': '3'}
+ p = properties.Property(schema)
+ self.assertEqual(p.validate_data(['1', '2', '3']), ['1', '2', '3'])
+
+ def test_list_smaller_than_minlength(self):
+ schema = {'Type': 'List',
+ 'MinLength': '4'}
+ p = properties.Property(schema)
+ self.assertRaises(ValueError, p.validate_data, ['1', '2', '3'])
+
def test_map_string(self):
p = properties.Property({'Type': 'Map'})
self.assertRaises(TypeError, p.validate_data, 'foo')
p = properties.Property({'Type': 'Map'})
self.assertRaises(TypeError, p.validate_data, ['foo'])
+ def test_map_maxlength_good(self):
+ schema = {'Type': 'Map',
+ 'MaxLength': '4'}
+ p = properties.Property(schema)
+ self.assertEqual(
+ p.validate_data({'1': 'one', '2': 'two', '3': 'three'}),
+ {'1': 'one', '2': 'two', '3': 'three'})
+
+ def test_map_exceeded_maxlength(self):
+ schema = {'Type': 'Map',
+ 'MaxLength': '2'}
+ p = properties.Property(schema)
+ self.assertRaises(ValueError,
+ p.validate_data,
+ {'1': 'one', '2': 'two', '3': 'three'})
+
+ def test_map_length_in_range(self):
+ schema = {'Type': 'Map',
+ 'MinLength': '2',
+ 'MaxLength': '4'}
+ p = properties.Property(schema)
+ self.assertEqual(
+ p.validate_data({'1': 'one', '2': 'two', '3': 'three'}),
+ {'1': 'one', '2': 'two', '3': 'three'})
+
+ def test_map_minlength_good(self):
+ schema = {'Type': 'Map',
+ 'MinLength': '2'}
+ p = properties.Property(schema)
+ self.assertEqual(p.validate_data({'1': 'one', '2': 'two'}),
+ {'1': 'one', '2': 'two'})
+
+ def test_map_smaller_than_minlength(self):
+ schema = {'Type': 'Map',
+ 'MinLength': '3'}
+ p = properties.Property(schema)
+ self.assertRaises(ValueError, p.validate_data, {'1': 'one'})
+
def test_map_schema_good(self):
map_schema = {'valid': {'Type': 'Boolean'}}
p = properties.Property({'Type': 'Map', 'Schema': map_schema})