bhch / django-jsonform

A better, user-friendly JSON editing form field for Django admin. Also supports Postgres ArrayField.
https://django-jsonform.rtfd.io
BSD 3-Clause "New" or "Revised" License
334 stars 34 forks source link

validators for jsonfield are not updated with dynamic schema #161

Open ReadMost opened 6 months ago

ReadMost commented 6 months ago

I want to update schema depending on separate field of model (e.g. "kind"). So there is a two main logic how to upload schema dynamically:

  1. when there is a new obj creation, we have to upload default schema unless user will change kind. I have used js api to track change event on container with id="id_kind" and update schema
    var mainForm = document.getElementById("id_kind");
    mainForm.addEventListener('change', onJsonFormChange);
  2. existing object is updated, then code must propagate existing schema with obj.kind. I have used callable schema and used js API for dynamic schema updating.

There is one issue related to validation, to be precise, jsonfield saves validation rules of schema which was loaded while python manage.py runserver. E.g. 1) callable schema returns default schema where maxItems: 1, when there is not kind selected 2) then user selects kind and other schema is updated via js, so now maxItems: 10, and user created 3 items and want to save. Then json field returns validation error as max allowed items is 1

bhch commented 6 months ago

Notes to self:

Issue: Schema changed on client-side via JS breaks validation on server-side because the server validator uses the older schema available on the server.

Possible solutions:

  1. Submit the modified schema along with the form data. But that would be a security concern as the user can basically provide any kind of schema they wish. So, we can't do this.
  2. Let the developer deal with this by writing their own custom validator. Seems to be the most reasonable option.

Bug: Developer's custom validator will not run because, currently, the default form field validation is always run. This will raise a validation error because it uses the older schema for validation. That means the developer's custom validator will not run.

Fix: There needs to be a way to disable the default validation. Maybe move the validate() method's logic into a validator and add it as default_validators = [...] on the field. That way the developer can disable it and also set their custom validators.

Breaking change: Yes. Changing the validate() method will break things if someone is overriding this method in their projects.

bhch commented 6 months ago

@ReadMost Hi, thank you for reporting this issue. This will take some time to fix (please see my previous comment).

Meanwhile, you can handle the validation using your custom validator and disable the default validation. For that, you'll need to override the form field:

from django_jsonform.forms.fields import JSONFormField

# Create a subclass of the form field
class MyJSONFormField(JSONFormField):
    def validate(self):
        # run grand-parent's validate method
        # skip parent's validate method
        # https://stackoverflow.com/a/43016980/1925257
        super(JSONFormField, self).validate()

# Now use your custom field in the form class
class MyModelForm(forms.ModelForm):
    field = MyJSONFormField(validators=[my_custom_validator])

# Now use this custom form on the admin site
class MyModelAdmin(admin.ModelAdmin):
    form = MyModelForm

Hopefully, this will help you until it gets fixed.

ReadMost commented 6 months ago

Hi, thanks a lot for reply. I will try soon and write about updates, ser