Open Lucidiot opened 1 year ago
excellent point @Lucidiot
Am I correct in understanding that this is supported by Django, but not "yet" by DRF? I think we can fix the exception, but to make it correct, DRF would also need to be changed.
DRF does need some fixes, and I mentioned them in this discussion on its repo, but since DRF still relies on Django's validators to perform the actual validation, it already almost works. You could use them as long as you don't look at the error messages 🙈
BTW, I ended up using a workaround that tricks DRF and Spectacular into not seeing the callable at all:
class HiddenCallableValidatorMixin(object):
def __init__(self, limit_value, message=None):
self._limit_value = limit_value
if message:
self.message = message
@property
def limit_value(self):
return self._limit_value() if callable(self._limit_value) else self._limit_value
class MaxValueValidator(HiddenCallableValidatorMixin, validators.MaxValueValidator):
pass
Describe the bug
I tried to use a function as the maximum value for an IntegerField in a Django model, since the validator docs mention that a callable is supported. I found that Spectacular crashes when generating a schema from that:
Note that I also found some small related issues in DRF itself which I reported here, but they are independent from this issue.
To Reproduce
Pick your poison! The Django model way:
Or the
min/max_value
way:Or the DRF validators way:
Then add the serializer to an API view and generate a schema from it in any way.
Expected behavior
The functions should have been called to get the minimum and maximum values right as the schema gets generated, so there would have been this field in the resulting schema: