{{{
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.
* 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>