[Django] #32286: Definition of JSONField causes issue with validation for a Falsey value

4 views
Skip to first unread message

Django

unread,
Dec 20, 2020, 8:11:15 PM12/20/20
to django-...@googlegroups.com
#32286: Definition of JSONField causes issue with validation for a Falsey value
-----------------------------------------+------------------------
Reporter: goodyguts | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
I ran into this issue while trying to create a JSONField that would
contain a list of strings. The list of strings could have any length. I
used the following:


{{{
model_field = models.JSONField(validator=[MyCustomValidator()])
}}}

The validator checks that the object is a list of strings along with some
other stuff.

However, under this configuration, `[]` was not a valid entry because it
was falsey and hence it failed the `blank=False` test.

So I set `blank=True` on the field to allow for `[]`. But this led to the
problem that if you have a falsey value then the validators aren't run. So
I was able to set the field to be `null` on the admin site and it was
permitted. But I really don't want to allow `null` as a value for fear of
causing bugs in my code that uses the API. Even worse an empty string `""`
was allowed!

As it is, as far as I can tell, there is no way to create a field level
validator that allows empty list and does not allow null. You could
probably use a model level validator but that is non-ideal, and this feels
like a potential way for bugs to emerge on a website.

--
Ticket URL: <https://code.djangoproject.com/ticket/32286>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 21, 2020, 1:31:56 AM12/21/20
to django-...@googlegroups.com
#32286: Definition of JSONField causes issue with validation for a Falsey value
---------------------------------+--------------------------------------
Reporter: Adam Carruthers | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: 3.1
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid
* component: Uncategorized => Forms


Comment:

Yes, Django doesn't run validators on empty values, however you can create
a custom form field and change `empty_values` to the `([],)`, see #19997
and
[https://github.com/django/django/blob/28d998a41c92bdac8eeae6509ecc87ed044b30d0/tests/forms_tests/tests/test_forms.py#L3045-L3064
an example in tests]. Please use one of
[https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels
support channels] if you have further questions.

--
Ticket URL: <https://code.djangoproject.com/ticket/32286#comment:1>

Reply all
Reply to author
Forward
0 new messages