add "minLength" support to the jsonschema validator

This commit is contained in:
Pēteris Caune 2018-10-29 17:13:45 +02:00
parent 0c6dcfa766
commit 887c4d534a
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2
2 changed files with 6 additions and 0 deletions

View File

@ -16,6 +16,8 @@ def validate(obj, schema, obj_name="value"):
if schema.get("type") == "string":
if not isinstance(obj, str):
raise ValidationError("%s is not a string" % obj_name)
if "minLength" in schema and len(obj) < schema["minLength"]:
raise ValidationError("%s is too short" % obj_name)
if "maxLength" in schema and len(obj) > schema["maxLength"]:
raise ValidationError("%s is too long" % obj_name)
if schema.get("format") == "cron":

View File

@ -12,6 +12,10 @@ class JsonSchemaTestCase(TestCase):
with self.assertRaises(ValidationError):
validate(123, {"type": "string"})
def test_it_checks_string_min_length(self):
with self.assertRaises(ValidationError):
validate("abcd", {"type": "string", "minLength": 5})
def test_it_checks_string_length(self):
with self.assertRaises(ValidationError):
validate("abcd", {"type": "string", "maxLength": 3})