Skip to content

Commit d734d2f

Browse files
committed
[fix] Attach netjsonconfig validation errors to config field
Fixes #744
1 parent 109f28e commit d734d2f

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

openwisp_controller/config/base/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def clean_netjsonconfig_backend(cls, backend):
203203
'Invalid configuration triggered by "#/{0}", '
204204
"validator says:\n\n{1}".format(trigger, error)
205205
)
206-
raise ValidationError(message)
206+
raise ValidationError({"config": message})
207207

208208
@cached_property
209209
def backend_class(self):

openwisp_controller/config/tests/test_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def test_netjson_validation(self):
152152
try:
153153
c.full_clean()
154154
except ValidationError as e:
155-
self.assertIn("Invalid configuration", e.message_dict["__all__"][0])
155+
self.assertIn("Invalid configuration", e.message_dict["config"][0])
156156
else:
157157
self.fail("ValidationError not raised")
158158

openwisp_controller/config/tests/test_template.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,36 @@ def test_task_called(self, mocked_task):
801801
template.save()
802802
mocked_task.assert_not_called()
803803

804+
def test_validation_fix_attached_to_config_field(self):
805+
"""
806+
Ensure netjsonconfig validation errors are attached to the 'config' field
807+
"""
808+
config = {
809+
"interfaces": [
810+
{
811+
"name": "vpn_test",
812+
"type": "openvpn",
813+
"mode": "server",
814+
# Missing required fields like keys, etc.
815+
}
816+
]
817+
}
818+
t = Template(
819+
name="validation_test",
820+
backend="netjsonconfig.OpenWrt",
821+
config=config,
822+
)
823+
try:
824+
t.full_clean()
825+
except ValidationError as e:
826+
self.assertIn("config", e.message_dict)
827+
self.assertIn(
828+
'Invalid configuration triggered by "#/interfaces/0"',
829+
e.message_dict["config"][0],
830+
)
831+
else:
832+
self.fail("ValidationError not raised")
833+
804834
@mock.patch.object(task_logger, "warning")
805835
def test_task_failure(self, mocked_warning):
806836
update_template_related_config_status.delay(uuid.uuid4())

0 commit comments

Comments
 (0)