Skip to content

Commit a691669

Browse files
committed
Add schema validation in CustomJSONField serializer
1 parent f124e60 commit a691669

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

django_custom_jsonfield/rest_framework/serializers.py

+9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
import jsonschema
22
from django.utils.translation import gettext_lazy as _
3+
from jsonschema import validators
34
from rest_framework import serializers
45

56

67
class CustomJSONField(serializers.JSONField):
78
default_error_messages = {
89
"invalid_data": _("Value does not match the JSON schema."),
10+
"invalid_schema": _("Invalid JSON schema."),
911
}
1012

1113
def __init__(self, schema: dict, **kwargs):
1214
self.schema = schema
1315
super().__init__(**kwargs)
16+
17+
validator = validators.validator_for(self.schema)
18+
try:
19+
validator.check_schema(self.schema)
20+
except jsonschema.exceptions.SchemaError:
21+
self.fail("invalid_schema")
22+
1423
self.validators.append(self._validate_data)
1524

1625
def _validate_data(self, value):

tests/test_serializer_field.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pytest
2-
from rest_framework import serializers
3-
from rest_framework.exceptions import ErrorDetail
2+
from rest_framework import exceptions
43

54
from django_custom_jsonfield.rest_framework.serializers import CustomJSONField
65

@@ -34,3 +33,22 @@ class FakeSerializer(serializers.Serializer):
3433
],
3534
}
3635
assert serializer.errors == expected_errors
36+
37+
38+
@pytest.mark.parametrize(
39+
"schema",
40+
[
41+
{"minItems": "1"},
42+
{"properties": 1},
43+
{"pattern": "*invalid.regex"},
44+
],
45+
)
46+
def test_map_serializer_field_invalid_schema(schema: dict):
47+
"""Test serializer raises an exception if JSON schema is invalid."""
48+
49+
with pytest.raises(exceptions.ValidationError) as e:
50+
CustomJSONField(schema=schema)
51+
52+
assert isinstance(e.value, exceptions.ValidationError)
53+
assert e.value.detail[0] == "Invalid JSON schema."
54+
assert e.value.detail[0].code == "invalid_schema"

0 commit comments

Comments
 (0)