-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5426126
commit 4a8a8b5
Showing
2 changed files
with
84 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from pydantic import BaseModel, EmailStr, Field | ||
from typing import List, Optional | ||
from datetime import datetime | ||
|
||
|
||
class User(BaseModel): | ||
id: int | ||
username: str = Field(..., min_length=3, max_length=50) | ||
email: EmailStr | ||
age: int = Field(..., ge=0, le=120) | ||
is_active: bool = True | ||
created_at: datetime = Field(default_factory=datetime.now) | ||
tags: List[str] = [] | ||
profile: Optional[dict] = None | ||
|
||
|
||
|
||
def main(): | ||
# Valid user | ||
try: | ||
user1 = User( | ||
id=1, | ||
username="JohnDoe", | ||
email="[email protected]", | ||
age=30, | ||
tags=["user", "premium"], | ||
profile={"bio": "Hello World"} | ||
) | ||
print("\nValid user:") | ||
print(user1.model_dump_json(indent=2)) | ||
|
||
except Exception as e: | ||
print(f"Error creating user1: {e}") | ||
|
||
# Invalid user (will raise validation error) | ||
try: | ||
user2 = User( | ||
id="invalid", # Should be int | ||
username="J", # Too short | ||
email="invalid-email", # Invalid email | ||
age=150, # Age too high | ||
) | ||
print(user2) | ||
except Exception as e: | ||
print("\nValidation errors:") | ||
print(e) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters