Skip to content

Commit 47e99c0

Browse files
committed
feat: invalid enum detection
1 parent c81a1b2 commit 47e99c0

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

fastapi_forge/dtos.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,33 @@ class ProjectSpec(_Base):
373373
models: list[Model] = []
374374
custom_enums: list[CustomEnum] = []
375375

376+
@model_validator(mode="after")
377+
def _validate_enums(self) -> Self:
378+
valid_enum_names = {custom_enum.name for custom_enum in self.custom_enums}
379+
380+
invalid_fields = [
381+
(model.name, field.name, field.type_enum)
382+
for model in self.models
383+
for field in model.fields
384+
if (
385+
field.type == FieldDataTypeEnum.ENUM
386+
and (field.type_enum is None or field.type_enum not in valid_enum_names)
387+
)
388+
]
389+
390+
if invalid_fields:
391+
error_lines = [
392+
f"• {model_name}.{field_name} (ref: '{type_enum}')"
393+
for model_name, field_name, type_enum in invalid_fields
394+
]
395+
raise ValueError(
396+
f"Invalid enum references ({len(invalid_fields)}):\n"
397+
+ "\n".join(error_lines)
398+
+ f"\nValid enums: {', '.join(sorted(valid_enum_names)) or 'none'}"
399+
)
400+
401+
return self
402+
376403
@model_validator(mode="after")
377404
def _validate_models(self) -> Self:
378405
model_names = [model.name for model in self.models]

fastapi_forge/project_io.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,13 @@ def _load_project_to_dict(self) -> dict[str, Any]:
247247
with self.project_path.open() as stream:
248248
return yaml.safe_load(stream)["project"]
249249

250-
def load(self) -> Any:
250+
def load(self) -> ProjectSpec:
251251
return ProjectSpec(**self._load_project_to_dict())
252252

253253
@classmethod
254-
def load_from_conn_string(cls, conn_string: str, schema: str = "public") -> Any:
254+
def load_from_conn_string(
255+
cls, conn_string: str, schema: str = "public"
256+
) -> ProjectSpec:
255257
db_info = _inspect_postgres_schema(conn_string, schema)
256258
db_schema = db_info["schema_data"]
257259
db_name = db_info["database_name"]

0 commit comments

Comments
 (0)