Path Parameters and Numeric Validations
Part of: FastAPI Basics
Learn to add validation constraints to path parameters using Path(). Set numeric ranges, add documentation, and create robust path parameter validation.
What You'll Learn
- Add validation constraints to path parameters
- Use Path() for advanced path parameter configuration
- Set numeric ranges for integer path parameters
- Add parameter documentation and metadata
- Combine Query and Path validations in the same endpoint
Theory and Concepts
Path Parameters and Numeric Validations
🎯 What You'll Learn
Following the official FastAPI tutorial, you'll learn how to add validation constraints, metadata, and documentation to your path parameters using FastAPI's Path function.
By the end of this lesson, you'll understand how to:
- Add validation constraints to path parameters
- Use Path() for advanced path parameter configuration
- Set numeric ranges for integer path parameters
- Add parameter documentation and metadata
- Combine Query and Path validations in the same endpoint
🛠️ The Path Function
Just like Query for query parameters, FastAPI provides the Path function to add metadata and validation constraints to path parameters.
Basic Path vs Advanced Path
Basic approach:
[Code Example]
Advanced approach with Path:
[Code Example]
🔢 Numeric Validation Constraints
Basic Numeric Constraints
[Code Example]
Validation:
- /items/1 ✅ Valid (ge=1 means >= 1)
- /items/0 ❌ Invalid (less than 1)
- /items/-5 ❌ Invalid (less than 1)
Multiple Constraints
[Code Example]
Available numeric constraints:
- ge: Greater than or equal
- gt: Greater than
- le: Less than or equal
- lt: Less than
💡 Official Tutorial Examples
Example 1: Basic Path Validation
From the official tutorial:
[Code Example]
Example 2: Path with Metadata
[Code Example]
Example 3: Combining Path and Query Validations
[Code Example]
🎨 Path Parameter Features
Validation Constraints
- ge: Greater than or equal (≥)
- gt: Greater than (>)
- le: Less than or equal (≤)
- lt: Less than (<)
Metadata and Documentation
- title: Parameter title in docs
- description: Parameter description
- deprecated: Mark parameter as deprecated
- include_in_schema: Include/exclude from OpenAPI schema
- example: Example value for documentation
Example with Full Metadata
[Code Example]
🔄 Order Matters: Path Before Query
When combining Path and Query parameters, you need to be careful about parameter order:
[Code Example]
Solution: Use * to Separate
[Code Example]
🌟 Advanced Path Validation
Float Path Parameters
[Code Example]
String Path Parameters with Validation
[Code Example]
✨ Best Practices
1. Use Reasonable Constraints
[Code Example]
2. Add Helpful Documentation
[Code Example]
3. Use Appropriate Data Types
[Code Example]
🚫 Common Beginner Mistakes
Mistake 1: Wrong Parameter Order
[Code Example]
Mistake 2: Forgetting to Import Path
[Code Example]
Mistake 3: Unrealistic Constraints
[Code Example]
🎉 What's Next?
Congratulations! You now understand how to add powerful validation and documentation to your path parameters. Combined with query parameter validation, you can create robust, well-documented APIs.
You've learned:
- How to use Path() for advanced path parameter configuration
- Adding numeric validation constraints (ge, le, gt, lt)
- Including parameter documentation and metadata
- Combining Path and Query validations in the same endpoint
Coming up next: Body - Multiple Parameters - learning how to handle multiple request body parameters and combine them with path and query parameters!
📖 Additional Resources
- FastAPI Path Parameters and Numeric Validations
- FastAPI Parameter Validation Guide
- OpenAPI Parameter Documentation
Helpful Hint
Import Path from fastapi and use it like: item_id: int = Path(ge=1, le=1000). This adds validation constraints to your path parameters!