Pydantic: UrlStr does not match e.g. kubernetes URLs

Created on 19 Jun 2019  路  5Comments  路  Source: samuelcolvin/pydantic

This URL does not pass validation: http://action-server/webhook. Any reason it should not?

Related to #246

Feedback Wanted feature request

Most helpful comment

Turns out this was possible all along, urlstr takes a require_tld argument that defaults to True, but can be set to False for cases like this.

However for v1 I've completely rewritten URL parsing to be easier to use and more powerful. Feedback very welcome on #755.

All 5 comments

I would also like such URLs to pass validation if possible just for purposes of local docker-compose development (even without kubernetes).

the short answer is, use DSN instead of url.

The long term answer is probably to merge DSN and URL and get it right.

Related to #541

For note, I got bitten by the same issue, for an URL pointing on a docker container in the same network (i.e. without TLD).
To reproduce :

from pydantic import UrlStr, dataclasses

@dataclasses.dataclass
class Test:
     uri: UrlStr

Test('http://kong/test.png')

This will throw a Validation error :

ValidationError: 1 validation error for Test
uri
  url string does not match regex (type=value_error.url.regex)

Trying with http://kong.fake-tld/test.png does not throw the validation error.

Using DSN instead of UrlStr is a workaround, but it removes the clarity and instinctiveness (no brainfuck) that can be achieved with pydantic on the rest : I found out about using DSN by digging into those GitHub issues, while the rest came naturally.

Could the URL validation regexp be modified to take into account this kind of URL ?

I've been thinking about this today, in v1 DSN and UrlStr should be unified and improved.

I guess the interface would be a bit like constr() and similar, something like:

  • AnyUrl virtually anything in a url structure, basically doesn't have spaces, has a scheme etc.
  • strict_url(schema='postgres') url where the scheme has to be postgres
  • HttpUrl an http(s) url, equivalent to strict_url(schema='https?') but cleaner and keeps mypy happy
  • HttpsUrl an https url, equivalent to strict_url(schema='https') but cleaner and keeps mypy happy
  • ... maybe others, eg. requiring a tld, requring/disallowing username and password

The output would always be a string.

Woud this work?

Turns out this was possible all along, urlstr takes a require_tld argument that defaults to True, but can be set to False for cases like this.

However for v1 I've completely rewritten URL parsing to be easier to use and more powerful. Feedback very welcome on #755.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marlonjan picture marlonjan  路  37Comments

bradodarb picture bradodarb  路  22Comments

kryft picture kryft  路  35Comments

Yolley picture Yolley  路  18Comments

cazgp picture cazgp  路  34Comments