-
-
Notifications
You must be signed in to change notification settings - Fork 38
Description
My goal is to to integrate a custom validator with ErrorMap support, i.e. one that raises a JSONSchemaValidationError. I expect the error messages to be displayed on the individual inputs like with the buildin schema validator.
Adding such a validator to the jsonform.JSONField like you would for model validation does not work. The validator is executed but the ErrorMap is apparently not propagated to the widget.
As far as I know there is a difference between (model-)field validators and form-field validators. From looking at the code I saw that jsonform.JSONFormField overrides django.forms.JSONField.run_validators to capture JSONSchemaValidationError s and propagate the ErrorMap.
jsonform.JSONField on the other hand does not override the corresponding django.db.models.JSONField.run_validator and thus ErrorMaps from model validators are not propagated to the widget.
I propose to add such an override.
I'm currently working around that issue by using my own model-field class and overriding .formfield() to inject the validator into the form-field validation pipeline instead of using the model-field validation pipeline.
class MyField(jsonform.JSONField):
SCHEMA = {
"type": "array",
"items": {"type": "string"},
"uniqueItems": True,
}
def __init__(self, *args, **kwargs):
super().__init__(schema=self.SCHEMA, *args, **kwargs_)
def formfield(self, **kwargs):
return super().formfield(**{"validators": [MyValidator()], **kwargs})