Skip to content

Add support for URL validator options to URLField #8895

Answered by nchz
psjamesh asked this question in General
Discussion options

You must be logged in to vote

For your particular example (allow only HTTPS) there's a clean and simple solution that doesn't require modifications in the URLField class:

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 URLValidator. But there's something interesting: If som…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@psjamesh
Comment options

Answer selected by psjamesh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants