Purpose
Ensures type safety and consistency between frontend TypeScript interfaces and backend Pydantic models by detecting field name differences, type mismatches, and missing fields.
When to Use
- When validating API contracts before a release.
- When identifying breaking changes in data models between frontend and backend.
- When standardizing property casing (camelCase vs snake_case) across the stack.
Process
-
Scan TypeScript interfaces:
- Read frontend API service files (
frontend/src/api/*.ts) - Extract TypeScript interfaces for requests/responses
- Parse field types, optional fields, arrays, enums
- Build TypeScript type inventory
- Read frontend API service files (
-
Scan Pydantic models:
- Read backend model files (
backend/app/models/*_schemas.py) - Extract Pydantic BaseModel definitions
- Parse field types, Optional types, Lists, Enums
- Build Python type inventory
- Read backend model files (
-
Match and compare contracts:
- Match TypeScript interfaces to Pydantic models by name
- Compare field names (check for camelCase vs snake_case)
- Compare field types (string vs str, number vs int/float)
- Check required vs optional field consistency
- Validate enum values match
-
Detect mismatches:
- Field name differences:
userIdvsuser_id - Type mismatches:
stringvsint - Missing fields: Field in frontend but not backend (or vice versa)
- Optional inconsistencies: Required in one but optional in another
- Enum value differences: Different allowed values
- Field name differences:
-
Generate validation report:
- Create
docs/API_CONTRACT_VALIDATION.mdwith:- Contract health score
- List of all validated contracts
- Detailed mismatch reports
- Recommended fixes (with code examples)
- Breaking vs non-breaking changes
- Create
-
Provide fix suggestions:
- Show exact code changes needed
- Generate conversion utilities if needed
- Suggest API versioning for breaking changes
Validation Report Structure
# API Contract Validation Report
Generated: 2025-01-06T12:00:00Z
## Summary
- Total Contracts Validated: 28
- β
Matching Contracts: 22 (78.6%)
- β οΈ Mismatches Found: 6 (21.4%)
- π΄ Breaking Issues: 2
- π‘ Non-Breaking Issues: 4
## Contract Health Score: 79/100
### β
VALID CONTRACTS (22)
| Contract Name | Frontend | Backend | Status |
| ----------------- | ----------------- | ------------------- | ---------------- |
| `ATSScoreRequest` | aiServices.ts | analysis_schemas.py | β
Perfect match |
| `UserProfile` | profileService.ts | schemas.py | β
Perfect match |
| ... |
### π΄ BREAKING MISMATCHES (2)
#### 1. NotificationPreferences
**Location:** `notificationService.ts` β `notification_schemas.py`
**Issues:**
- Field type mismatch: `frequency` is `string` in TS but `int` in Python
- Missing required field: `user_id` required in backend but not sent from frontend
**Impact:** π΄ API calls will fail with 422 validation errors
**Fix (Frontend):**
```typescript
// notificationService.ts
interface NotificationPreferences {
frequency: number; // Change from string to number
user_id: string; // Add missing field
// ... other fields
}
```
Fix (Backend - Alternative):
# notification_schemas.py
class NotificationPreferencesRequest(BaseModel):
frequency: str # Change from int to str
# Remove user_id from request, get from auth instead
π‘ NON-BREAKING WARNINGS (4)
1. Casing Inconsistency
Issue: Frontend uses camelCase, backend uses snake_case Affected: 15 contracts Impact: π‘ Works but inconsistent (Pydantic auto-converts) Recommendation: Standardize on one casing style
Example:
// Frontend (camelCase)
interface JobListing {
jobTitle: string;
companyName: string;
}
// Backend (snake_case)
class JobListingResponse(BaseModel):
job_title: str
company_name: str
Recommendation: Add Pydantic alias config:
class JobListingResponse(BaseModel):
job_title: str = Field(alias="jobTitle")
company_name: str = Field(alias="companyName")
class Config:
populate_by_name = True
Type Mapping Reference
| TypeScript | Python (Pydantic) | Compatible |
| ----------- | ----------------- | ---------------------- |
| string | str | β
|
| number | int, float | β
|
| boolean | bool | β
|
| string[] | List[str] | β
|
| Date | datetime | β οΈ Needs serialization |
| any | Any | β οΈ Avoid if possible |
| T \| null | Optional[T] | β
|
## Usage Tips
- Run validator before major releases
- Integrate into CI/CD pipeline
- Fix breaking issues immediately
- Schedule non-breaking fixes for next sprint
- Use validator output for API documentation