#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.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.89cd14f655a8484e028ef85bff31348b%40djangoproject.com.