返回 Skill 列表
extension
分类: 开发与工程无需 API Key

api-request-validation

一种技能,用于在像FastAPI这样的Python Web框架中使用Pydantic实现强大的API请求验证。涵盖Pydantic模型、自定义验证器(电子邮件、密码)、字段级和跨字段验证、查询/文件验证以及结构化的错误响应。当您需要验证传入的API请求时,请使用此技能。

person作者: jakexiaohubgithub

API Request Validation Skill

Overview

This skill provides guidance and reusable components for implementing robust API request validation in Python, primarily using FastAPI and Pydantic.

It covers:

  • Basic validation with Pydantic models.
  • Custom validation logic for specific fields.
  • Advanced validation scenarios like cross-field validation and query parameters.
  • Standardized error handling for validation failures.

Getting Started

The most common validation tasks are broken down into reference files. Start with the one that best fits your needs.

  • Basic Validation: If you're new to Pydantic or need to validate a simple data structure, see references/pydantic-models.md.
  • Custom Rules: For validating specific formats like emails or password strength, see references/custom-validators.md.
  • Complex Scenarios: For validating query parameters, file uploads, or dependencies between fields, see references/advanced-validation.md.
  • Error Responses: To customize how validation errors are returned to the client, see references/error-handling.md.

Resources

references/

  • pydantic-models.md: Guide to creating basic Pydantic models for request body validation.
  • custom-validators.md: How to write custom validator functions for fields.
  • advanced-validation.md: Covers cross-field validation, query parameters, and file uploads.
  • error-handling.md: Instructions for creating a custom FastAPI exception handler for validation errors.

scripts/

  • validators.py: A collection of reusable Pydantic validator functions (e.g., for email, password strength).
  • error_handler.py: A pre-built FastAPI exception handler for RequestValidationError.
  • main_example.py: A full FastAPI application demonstrating all validation techniques covered in this skill. You can run this file to see the validation in action.