Skip to content

Commit

Permalink
Add NatsDsn to networks.py (#6874)
Browse files Browse the repository at this point in the history
Co-authored-by: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com>
  • Loading branch information
ekeew and sydney-runkle committed Nov 13, 2023
1 parent d6140fa commit a168d5f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pydantic/__init__.py
Expand Up @@ -117,6 +117,7 @@
'RedisDsn',
'MongoDsn',
'KafkaDsn',
'NatsDsn',
'MySQLDsn',
'MariaDBDsn',
'validate_email',
Expand Down Expand Up @@ -263,6 +264,7 @@
'RedisDsn': (__package__, '.networks'),
'MongoDsn': (__package__, '.networks'),
'KafkaDsn': (__package__, '.networks'),
'NatsDsn': (__package__, '.networks'),
'MySQLDsn': (__package__, '.networks'),
'MariaDBDsn': (__package__, '.networks'),
'validate_email': (__package__, '.networks'),
Expand Down
11 changes: 11 additions & 0 deletions pydantic/networks.py
Expand Up @@ -40,6 +40,7 @@
'RedisDsn',
'MongoDsn',
'KafkaDsn',
'NatsDsn',
'validate_email',
'MySQLDsn',
'MariaDBDsn',
Expand Down Expand Up @@ -309,6 +310,16 @@ def check_db_name(cls, v):
* TLD not required
* Host required
"""
NatsDsn = Annotated[
MultiHostUrl, UrlConstraints(allowed_schemes=['nats', 'tls', 'ws'], default_host='localhost', default_port=4222)
]
"""A type that will accept any NATS DSN.
NATS is a connective technology built for the ever increasingly hyper-connected world.
It is a single technology that enables applications to securely communicate across
any combination of cloud vendors, on-premise, edge, web and mobile, and devices.
More: https://nats.io
"""
MySQLDsn = Annotated[
Url,
UrlConstraints(
Expand Down
18 changes: 18 additions & 0 deletions tests/test_networks.py
Expand Up @@ -16,6 +16,7 @@
MongoDsn,
MySQLDsn,
NameEmail,
NatsDsn,
PostgresDsn,
RedisDsn,
Strict,
Expand Down Expand Up @@ -661,6 +662,23 @@ class Model(BaseModel):
assert m.a.password is None


@pytest.mark.parametrize(
'dsn,result',
[
('nats://user:pass@localhost:4222', 'nats://user:pass@localhost:4222'),
('tls://user@localhost', 'tls://user@localhost:4222'),
('ws://localhost:2355', 'ws://localhost:2355/'),
('tls://', 'tls://localhost:4222'),
('ws://:password@localhost:9999', 'ws://:password@localhost:9999/'),
],
)
def test_nats_dsns(dsn, result):
class Model(BaseModel):
dsn: NatsDsn

assert str(Model(dsn=dsn).dsn) == result


def test_custom_schemes():
class Model(BaseModel):
v: Annotated[Url, UrlConstraints(allowed_schemes=['ws', 'wss']), Strict()]
Expand Down

0 comments on commit a168d5f

Please sign in to comment.