Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validators for jsonfield are not updated with dynamic schema #161

Open
ReadMost opened this issue Mar 19, 2024 · 3 comments
Open

validators for jsonfield are not updated with dynamic schema #161

ReadMost opened this issue Mar 19, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@ReadMost
Copy link

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);
  1. 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 bhch added the bug Something isn't working label Mar 19, 2024
@bhch
Copy link
Owner

bhch commented Mar 19, 2024

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
Copy link
Owner

bhch commented Mar 19, 2024

@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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants