https://github.com/sourabh-kumar04/pydantic-basic
A comprehensive collection of examples demonstrating Pydantic's capabilities for data validation and modeling in Python, including integration with FastAPI.
https://github.com/sourabh-kumar04/pydantic-basic
fastapi field-constraints learning nested-models pydantic python serialization validator
Last synced: 4 months ago
JSON representation
A comprehensive collection of examples demonstrating Pydantic's capabilities for data validation and modeling in Python, including integration with FastAPI.
- Host: GitHub
- URL: https://github.com/sourabh-kumar04/pydantic-basic
- Owner: Sourabh-Kumar04
- License: apache-2.0
- Created: 2025-05-02T17:11:01.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2025-05-03T21:31:02.000Z (10 months ago)
- Last Synced: 2025-09-09T13:03:38.079Z (5 months ago)
- Topics: fastapi, field-constraints, learning, nested-models, pydantic, python, serialization, validator
- Language: Python
- Homepage:
- Size: 25.4 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ๐งฐ Pydantic-Basic
Welcome to **Pydantic-Basic**, a curated collection of examples demonstrating the capabilities of [Pydantic](https://docs.pydantic.dev/latest/) for data validation and modeling in Python. This repository is designed to help developers understand and utilize Pydantic's features effectively, from foundational concepts to integration with FastAPI.
---
## ๐ Repository Structure
```
Pydantic-Basic/
โโโ 01\_foundation/
โโโ 02\_fields/
โโโ 03\_model\_behaviour/
โโโ 04\_nested\_model/
โโโ 05\_serialization/
โโโ fastapi/
โโโ main.py
โโโ README.md
โโโ LICENSE
โโโ pyproject.toml
โโโ uv.lock
````
- **01_foundation/**: Introduction to basic Pydantic models and type annotations.
- **02_fields/**: Exploration of field types, default values, and field constraints.
- **03_model_behaviour/**: Understanding model behaviors, including validators and computed fields.
- **04_nested_model/**: Working with nested and recursive models.
- **05_serialization/**: Demonstrating serialization and custom JSON encoding.
- **fastapi/**: Integrating Pydantic models with FastAPI for building APIs.
---
## ๐ Getting Started
### Prerequisites
Ensure you have Python 3.9 or higher installed. You can check your Python version using:
```bash
python --version
````
### Installation
1. **Clone the repository:**
```bash
git clone https://github.com/Sourabh-Kumar04/Pydantic-Basic.git
cd Pydantic-Basic
```
2. **Install `uv`:**
If you haven't installed `uv` yet, you can do so using pip:
```bash
pip install uv
```
3. **Install project dependencies:**
```bash
uv sync
```
This command will install all the dependencies specified in `pyproject.toml` and lock them in `uv.lock`.
---
## ๐งช Usage
Each directory contains Python scripts that can be run independently to observe Pydantic's features in action.
For example, to run the foundational examples:
```bash
uv run 01_foundation/example.py
```
To start the FastAPI application:
```bash
uv run fastapi/fastapi_examples.py
```
---
## ๐ Examples Overview
### โ
01\_foundation
* Basic model creation using `BaseModel`.
* Type enforcement and automatic data parsing.
### ๐ ๏ธ 02\_fields
* Field definitions with default values.
* Field constraints and aliases.
### ๐ 03\_model\_behaviour
* Custom validators using `@validator`.
* Computed fields with `@property`.
### ๐งฉ 04\_nested\_model
* Nested models to represent complex data structures.
* Recursive models for hierarchical data like comments.
### ๐ค 05\_serialization
* Model serialization to dictionaries and JSON.
* Custom JSON encoders for complex types like `datetime`.
### ๐ fastapi
* Building API endpoints with FastAPI.
* Request and response models using Pydantic.
---
## ๐ License
This project is licensed under the [Apache-2.0 License](LICENSE).
---
## ๐โโ๏ธ Author
**Sourabh Kumar**
* ๐ผ [LinkedIn](https://linkedin.com/in/sourabh-kumar04)
* ๐ง [Email](mailto:sourabh404sourabh@gmail.com)
Feel free to reach out for collaborations or queries related to this project.
---
## โญ Acknowledgments
* [Pydantic Documentation](https://docs.pydantic.dev/latest/)
* [FastAPI Documentation](https://fastapi.tiangolo.com/)
* [uv Documentation](https://docs.astral.sh/uv/)
---
```
This `README.md` reflects the use of `uv` for dependency management and running the FastAPI application. If you need assistance with setting up `pyproject.toml` or have further questions, feel free to ask!
```