Sibyx / django_api_forms

Declarative Django request validation for RESTful APIs

Home Page:https://sibyx.github.io/django_api_forms/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Empty cleaned data in clean method

zurek11 opened this issue · comments

I would like to check, if data are valid in base_64 encoded message. This process is made in clean_items_base64 method. But when this method is processed, now method clean have cleaned_data as empty dict.

If I remove whole clean_items_base64 method, clean works and cleaned_data are there.

We need to write test, which duplicate this issue. After that, we need to find reason, fix it and make that test to run successfully.

Example:

class Sign(Form):
    class QrCodeForm(Form):
        items_base64 = forms.CharField(required=True)

        def clean_items_base64(self):
            items_base64 = self.cleaned_data.get('items_base64')
            decoded_items_base64 = base64.b64decode(items_base64.encode()).decode()
            items = json.loads(decoded_items_base64)

            for item in items:
                amount = item.get('amount', 0)
                item_category_id = item.get('item_category_id')

                if not amount or amount == 0:
                    self.add_error(
                        ('items_base64',),
                        ValidationError(
                            _("Amount can't be zero"),
                            code='office-not-price-list'
                        )
                    )
                if not item_category_id:
                    self.add_error(
                        ('items_base64',),
                        ValidationError(
                            _('Item category identification is required'),
                            code='office-not-price-list'
                        )
                    )

            return items_base64

        payload = FormField(form=QrCodeForm, required=True)

class Synchronize(Form):
    class Item(Form):
        name = forms.CharField(required=False)
        sign = FormField(form=Sign, required=True)
    plans = FormFieldList(form=Item, required=True)
    
    def clean(self):
        # Here are cleaned_data empty dict: {}
        for index, item in enumerate(self.cleaned_data.get('plans', [])):
            pass

        return self.cleaned_data