Skip to content

Commit 6182cd7

Browse files
authored
Merge pull request lightspeed-core#415 from tisnik/lcore-467-tcp-ports-configuration
LCORE-467: TCP ports type should be positive int
2 parents d6a0767 + 501484a commit 6182cd7

4 files changed

Lines changed: 9 additions & 15 deletions

File tree

docs/config.png

814 Bytes
Loading

docs/config.puml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class "PostgreSQLDatabaseConfiguration" as src.models.config.PostgreSQLDatabaseC
7373
host : str
7474
namespace : Optional[str]
7575
password : str
76-
port : int
76+
port : Annotated
7777
ssl_mode : str
7878
user : str
7979
check_postgres_configuration() -> Self
@@ -87,9 +87,9 @@ class "ServiceConfiguration" as src.models.config.ServiceConfiguration {
8787
color_log : bool
8888
cors
8989
host : str
90-
port : int
90+
port : Annotated
9191
tls_config
92-
workers : int
92+
workers : Annotated
9393
check_service_configuration() -> Self
9494
}
9595
class "TLSConfiguration" as src.models.config.TLSConfiguration {

src/models/config.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pathlib import Path
44
from typing import Optional
55

6-
from pydantic import BaseModel, model_validator, FilePath, AnyHttpUrl
6+
from pydantic import BaseModel, model_validator, FilePath, AnyHttpUrl, PositiveInt
77
from typing_extensions import Self, Literal
88

99
import constants
@@ -58,7 +58,7 @@ class PostgreSQLDatabaseConfiguration(BaseModel):
5858
"""PostgreSQL database configuration."""
5959

6060
host: str = "localhost"
61-
port: int = 5432
61+
port: PositiveInt = 5432
6262
db: str
6363
user: str
6464
password: str
@@ -70,8 +70,6 @@ class PostgreSQLDatabaseConfiguration(BaseModel):
7070
@model_validator(mode="after")
7171
def check_postgres_configuration(self) -> Self:
7272
"""Check PostgreSQL configuration."""
73-
if self.port <= 0:
74-
raise ValueError("Port value should not be negative")
7573
if self.port > 65535:
7674
raise ValueError("Port value should be less than 65536")
7775
if self.ca_cert_path is not None and not self.ca_cert_path.exists():
@@ -124,9 +122,9 @@ class ServiceConfiguration(BaseModel):
124122
"""Service configuration."""
125123

126124
host: str = "localhost"
127-
port: int = 8080
125+
port: PositiveInt = 8080
128126
auth_enabled: bool = False
129-
workers: int = 1
127+
workers: PositiveInt = 1
130128
color_log: bool = True
131129
access_log: bool = True
132130
tls_config: TLSConfiguration = TLSConfiguration()
@@ -135,12 +133,8 @@ class ServiceConfiguration(BaseModel):
135133
@model_validator(mode="after")
136134
def check_service_configuration(self) -> Self:
137135
"""Check service configuration."""
138-
if self.port <= 0:
139-
raise ValueError("Port value should not be negative")
140136
if self.port > 65535:
141137
raise ValueError("Port value should be less than 65536")
142-
if self.workers < 1:
143-
raise ValueError("Workers must be set to at least 1")
144138
return self
145139

146140

tests/unit/models/test_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_service_configuration_constructor() -> None:
4848

4949
def test_service_configuration_port_value() -> None:
5050
"""Test the ServiceConfiguration port value validation."""
51-
with pytest.raises(ValueError, match="Port value should not be negative"):
51+
with pytest.raises(ValidationError, match="Input should be greater than 0"):
5252
ServiceConfiguration(port=-1)
5353

5454
with pytest.raises(ValueError, match="Port value should be less than 65536"):
@@ -57,7 +57,7 @@ def test_service_configuration_port_value() -> None:
5757

5858
def test_service_configuration_workers_value() -> None:
5959
"""Test the ServiceConfiguration workers value validation."""
60-
with pytest.raises(ValueError, match="Workers must be set to at least 1"):
60+
with pytest.raises(ValidationError, match="Input should be greater than 0"):
6161
ServiceConfiguration(workers=-1)
6262

6363

0 commit comments

Comments
 (0)