Demonstrate Pydantic model validation capabilities
()
| 687 | |
| 688 | |
| 689 | def demo_model_validation() -> None: |
| 690 | """Demonstrate Pydantic model validation capabilities""" |
| 691 | print("\n🧪 TESTING PYDANTIC MODEL VALIDATION") |
| 692 | |
| 693 | # Define valid platform data |
| 694 | valid_platform_data: dict[str, Any] = { |
| 695 | "name": "ESP32 Arduino", |
| 696 | "architecture": "esp32", |
| 697 | "version": "2.0.5", |
| 698 | "category": "ESP32", |
| 699 | "url": "https://github.com/espressif/arduino-esp32/releases/download/2.0.5/esp32-2.0.5.zip", |
| 700 | "archiveFileName": "esp32-2.0.5.zip", |
| 701 | "checksum": "SHA-256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", |
| 702 | "size": "50000000", # Should convert to MB |
| 703 | "boards": [], |
| 704 | "toolsDependencies": [], |
| 705 | "help": {"online": "https://github.com/espressif/arduino-esp32"}, |
| 706 | } |
| 707 | |
| 708 | # Test valid platform |
| 709 | try: |
| 710 | platform = Platform(**valid_platform_data) |
| 711 | print(f"✅ Valid platform created: {platform.name} v{platform.version}") |
| 712 | print(f" Size converted: {platform.size_mb:.1f} MB") |
| 713 | |
| 714 | except ValidationError as e: |
| 715 | print(f"❌ Unexpected validation error: {e}") |
| 716 | |
| 717 | # Test invalid platform |
| 718 | try: |
| 719 | invalid_platform_data = valid_platform_data.copy() |
| 720 | invalid_platform_data["checksum"] = "invalid-checksum-format" |
| 721 | |
| 722 | platform = Platform(**invalid_platform_data) |
| 723 | print("❌ Should have failed validation!") |
| 724 | |
| 725 | except ValidationError as e: |
| 726 | print(f"✅ Correctly caught invalid checksum: {e.errors()[0]['msg']}") |
| 727 | |
| 728 | # Test invalid URL |
| 729 | try: |
| 730 | invalid_url_data = valid_platform_data.copy() |
| 731 | invalid_url_data["url"] = "not-a-valid-url" |
| 732 | |
| 733 | platform = Platform(**invalid_url_data) |
| 734 | print("❌ Should have failed URL validation!") |
| 735 | |
| 736 | except ValidationError as e: |
| 737 | print(f"✅ Correctly caught invalid URL: {e.errors()[0]['msg']}") |
| 738 | |
| 739 | |
| 740 | def main() -> None: |