Skip to content

Commit 168f31c

Browse files
committed
fix: add field validation to Settings config (#10)
- storage_mode: str → Literal["lite", "full"] (invalid values now caught at startup) - text_density_threshold: validated to 0.0–1.0 range - port: validated to 1–65535 range
1 parent 61eafae commit 168f31c

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

token0/config.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
from typing import Literal
2+
3+
from pydantic import field_validator
14
from pydantic_settings import BaseSettings
25

36

47
class Settings(BaseSettings):
58
# Storage mode: "lite" (SQLite + in-memory) or "full" (Postgres + Redis + S3)
69
# Use "lite" for local dev/testing, "full" for production
7-
storage_mode: str = "lite"
10+
storage_mode: Literal["lite", "full"] = "lite"
811

912
# Database — only needed in full mode
1013
database_url: str = "postgresql+asyncpg://token0:token0@localhost:5432/token0"
@@ -40,6 +43,20 @@ class Settings(BaseSettings):
4043
jpeg_quality: int = 85
4144
text_density_threshold: float = 0.52 # Above this → OCR route instead of vision
4245

46+
@field_validator("text_density_threshold")
47+
@classmethod
48+
def validate_threshold(cls, v: float) -> float:
49+
if not 0.0 <= v <= 1.0:
50+
raise ValueError(f"text_density_threshold must be between 0.0 and 1.0, got {v}")
51+
return v
52+
53+
@field_validator("port")
54+
@classmethod
55+
def validate_port(cls, v: int) -> int:
56+
if not 1 <= v <= 65535:
57+
raise ValueError(f"port must be between 1 and 65535, got {v}")
58+
return v
59+
4360
@property
4461
def is_lite(self) -> bool:
4562
return self.storage_mode == "lite"

0 commit comments

Comments
 (0)