diff --git a/hc/api/schemas.py b/hc/api/schemas.py index 54787071..a57aaaa2 100644 --- a/hc/api/schemas.py +++ b/hc/api/schemas.py @@ -1,8 +1,8 @@ check = { "type": "object", "properties": { - "name": {"type": "string"}, - "tags": {"type": "string"}, + "name": {"type": "string", "maxLength": 100}, + "tags": {"type": "string", "maxLength": 500}, "timeout": {"type": "number", "minimum": 60, "maximum": 604800}, "grace": {"type": "number", "minimum": 60, "maximum": 604800}, "channels": {"type": "string"}, diff --git a/hc/api/tests/test_create_check.py b/hc/api/tests/test_create_check.py index e787ee12..9c018a42 100644 --- a/hc/api/tests/test_create_check.py +++ b/hc/api/tests/test_create_check.py @@ -111,6 +111,10 @@ class CreateCheckTestCase(BaseTestCase): self.post({"api_key": "abc", "name": False}, expected_fragment="name is not a string") + def test_it_rejects_long_name(self): + self.post({"api_key": "abc", "name": "01234567890" * 20}, + expected_fragment="name is too long") + def test_unique_accepts_only_whitelisted_values(self): existing = Check(user=self.alice, name="Foo") existing.save() diff --git a/hc/lib/jsonschema.py b/hc/lib/jsonschema.py index 1e5842a4..6e7be664 100644 --- a/hc/lib/jsonschema.py +++ b/hc/lib/jsonschema.py @@ -15,6 +15,8 @@ def validate(obj, schema, obj_name="value"): if schema.get("type") == "string": if not isinstance(obj, string_types): raise ValidationError("%s is not a string" % obj_name) + if "maxLength" in schema and len(obj) > schema["maxLength"]: + raise ValidationError("%s is too long" % obj_name) elif schema.get("type") == "number": if not isinstance(obj, int): diff --git a/hc/lib/tests/test_jsonschema.py b/hc/lib/tests/test_jsonschema.py index 65e10b5f..2ac32402 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_length(self): + with self.assertRaises(ValidationError): + validate("abcd", {"type": "string", "maxLength": 3}) + def test_it_validates_numbers(self): validate(123, {"type": "number", "minimum": 0, "maximum": 1000})