Skip to content

Commit f0b92a5

Browse files
committed
ENH: Raise exception on incorrect props
1 parent 3b5aca1 commit f0b92a5

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
class ExampleError(Exception):
2-
"""Example for custom exception definition"""
1+
class InvalidPropError(ValueError):
2+
"""Raised when props are incorrectly configured"""

django_svelte_jsoneditor/settings.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from django.conf import settings
22

3+
from .exceptions import InvalidPropError
4+
35

46
DEFAULT_SVELTE_JSONEDITOR_PROPS = {
57
"mode": "tree",
@@ -16,9 +18,23 @@
1618
}
1719

1820

21+
_AVAILABLE_PROPS = set(DEFAULT_SVELTE_JSONEDITOR_PROPS.keys())
22+
23+
24+
def check_props(props):
25+
"""Check that props given to the widget are valid"""
26+
for key in props:
27+
if key not in _AVAILABLE_PROPS:
28+
raise InvalidPropError(f"Invalid prop '{key}'")
29+
30+
return props
31+
32+
1933
def get_props():
2034
"""Get default props overridden by any props set in the SVELTE_JSONEDITOR_PROPS setting"""
21-
return {
22-
**DEFAULT_SVELTE_JSONEDITOR_PROPS,
23-
**getattr(settings, "SVELTE_JSONEDITOR_PROPS", {}),
24-
}
35+
return check_props(
36+
{
37+
**DEFAULT_SVELTE_JSONEDITOR_PROPS,
38+
**getattr(settings, "SVELTE_JSONEDITOR_PROPS", {}),
39+
}
40+
)

django_svelte_jsoneditor/widgets.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
from django.forms import Textarea
33

4-
from .settings import get_props
4+
from .settings import check_props, get_props
55

66

77
class SvelteJSONEditorWidget(Textarea):
@@ -12,6 +12,7 @@ def __init__(self, props=None, attrs=None):
1212
attrs = {}
1313

1414
self.props = {} if props is None else props.copy()
15+
check_props(self.props)
1516
attrs.update({"class": "hidden"})
1617

1718
super().__init__(attrs)

tests/test_widget.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.contrib.auth.models import User
88
from django.test import Client, TestCase
99
from django.test.utils import override_settings
10+
from django_svelte_jsoneditor.exceptions import InvalidPropError
1011
from django_svelte_jsoneditor.widgets import SvelteJSONEditorWidget
1112

1213
from tests.server.example.models import ExampleBlankJsonFieldModel, ExampleJsonFieldModel
@@ -99,3 +100,15 @@ class SvelteJsonEditorForm(forms.Form):
99100

100101
form = SvelteJsonEditorForm()
101102
self.assertIn('"readOnly": false', str(form["my_json"]))
103+
104+
@override_settings(SVELTE_JSONEDITOR_PROPS={**{"incorrectProp": True}})
105+
def test_svelte_jsoneditor_widget_incorrect_props(self):
106+
107+
# for props at the widget level
108+
with self.assertRaises(InvalidPropError):
109+
SvelteJSONEditorWidget(props={"incorrectProp": True})
110+
111+
# for props in settings
112+
with self.assertRaises(InvalidPropError):
113+
widget = SvelteJSONEditorWidget()
114+
widget.get_context("my_json", "null", {"required": True, "id": "id_my_json"})

0 commit comments

Comments
 (0)