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 = (
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):
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,