Browse Source

add "minLength" support to the jsonschema validator

pull/199/head
Pēteris Caune 6 years ago
parent
commit
887c4d534a
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
2 changed files with 6 additions and 0 deletions
  1. +2
    -0
      hc/lib/jsonschema.py
  2. +4
    -0
      hc/lib/tests/test_jsonschema.py

+ 2
- 0
hc/lib/jsonschema.py 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":


+ 4
- 0
hc/lib/tests/test_jsonschema.py 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})


Loading…
Cancel
Save