-
It would be useful it the For example, I would like to verify that a URL is HTTPS only. It would be helpful to define this in the field, eg:
This would be a very simple change, adding flexibility to the field to avoid including extra validation elsewhere in the application.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
For your particular example (allow only HTTPS) there's a clean and simple solution that doesn't require modifications in the from django.core.validators import URLValidator
from rest_framework import serializers
class MySerializer(serializers.Serializer):
some_url = serializers.URLField(
validators=[URLValidator(schemes=["https"])],
)
assert not MySerializer(data={"some_url": "http://example.com"}).is_valid()
assert MySerializer(data={"some_url": "https://example.com"}).is_valid() Nevertheless, you made me realize that this works because your schemes of interest are a subset of the default schemes of I propose this answer because it may help in your use case, and I also give +1 because the feature seems to be useful in other scenarios. |
Beta Was this translation helpful? Give feedback.
For your particular example (allow only HTTPS) there's a clean and simple solution that doesn't require modifications in the
URLField
class:Nevertheless, you made me realize that this works because your schemes of interest are a subset of the default schemes of
URLValidator
. But there's something interesting: If som…