A FastAPI project with custom input validation (email, phone, password), PostgreSQL integration, and database-stored logs.
- FastAPI
- Pydantic
- PostgreSQL
- SQLAlchemy
- Decorators for validation
- Logs stored directly in a logs table
-
Clone the repo and enter the folder
-
Create and activate virtual environment
-
Install dependencies
-
Create a PostgreSQL database
Make sure this matches the connection string in
database.pyDATABASE_URL = "postgresql://postgres:root@localhost/fastapi_db" -
Run the application
uvicorn main:app --reload
-
Open your browser and go to
http://127.0.0.1:8000/docs -
Click on
/register→ Try it out → Enter sample data -
Click Execute — if all inputs are valid, it will be saved to the database.
-
Users Table
SELECT * FROM users;
-
Validation & DB Logs Table
SELECT * FROM logs;
- main.py – App startup, route definitions, request model, and usage of custom validation decorators.
- validators.py – Contains custom decorators to validate email, phone, and password input using regex, with logging.
- db.py – Handles saving user data and validation logs into the PostgreSQL database using SQLAlchemy.
- models.py – Defines SQLAlchemy ORM models for the
usersandlogstables. - database.py – Initializes the database engine, session factory, and base class for ORM models.
- logger.py – (Optional) Previously used for file-based logging; replaced with database logging logic.
- log_utils.py – Contains logic to save validation logs into the database instead of writing to log files.
- requirements.txt – Lists all the dependencies needed to install and run the project (FastAPI, SQLAlchemy, psycopg2).