diff --git a/hc/lib/jsonschema.py b/hc/lib/jsonschema.py index 86f2df35..53e33371 100644 --- a/hc/lib/jsonschema.py +++ b/hc/lib/jsonschema.py @@ -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": diff --git a/hc/lib/tests/test_jsonschema.py b/hc/lib/tests/test_jsonschema.py index d51a120c..88a08990 100644 --- a/hc/lib/tests/test_jsonschema.py +++ b/hc/lib/tests/test_jsonschema.py @@ -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})