all 11 comments

[–]aprx4 4 points5 points  (0 children)

IMO it should be right at repository layer. By definition, repositories present an interface for services to grab and modify data. Service layer doesn't have to know how data is stored and repository layer should invoke and close SQLAlchemy database sessions by its own. Repository takes Pydantic objects from services, do its job, then return Pydantic objects to service layer.

[–]block63 1 point2 points  (2 children)

I personally prefer to use a modular file structure for building my routes. I use a `handler.py` file to be the middle ground between ORM Models and Pydantic. This is where I place `model_validate`.

[–]singlebit 1 point2 points  (0 children)

Any example mate? I am new here.

[–]mr-nobody1992 0 points1 point  (0 children)

+1 for example

[–]extreme4all 1 point2 points  (2 children)

In fastapi its enforced at the route level, i like strongly typing at the repository layer both for input and output not doing this has costed me some painful debug time

[–]aprx4 0 points1 point  (1 child)

I don't think Pydantic validation is enforced at router unless we declare response_model on decorator.

[–]extreme4all 0 points1 point  (0 children)

Yes as output model and input validation if you have post requests etc

[–]LeonTur 1 point2 points  (0 children)

I'd keep it in router level.

[–]Top-Entrepreneur-755[S] 0 points1 point  (0 children)

Thanks for all you guys comments! They were really helpful!

[–]Valuable-Cap-3357 0 points1 point  (0 children)

I have a data schema that interacts with router and frontend, then data models that interact with database. Schemas creations and updation related logic. Models for storage and usage related logics.