Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NATS DSN #6874

Merged
merged 7 commits into from Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pydantic/__init__.py
Expand Up @@ -117,6 +117,7 @@
'RedisDsn',
'MongoDsn',
'KafkaDsn',
'NatsDsn',
'MySQLDsn',
'MariaDBDsn',
'validate_email',
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