fiobank icon indicating copy to clipboard operation
fiobank copied to clipboard

Replace custom schemas with pydantic models for improved validation

Open Copilot opened this issue 9 months ago • 7 comments

This PR modernizes the FioBank library by replacing custom transaction and info schemas with Pydantic models, significantly simplifying the codebase while improving type safety and maintainability.

Changes

Removed complex custom schemas:

  • Eliminated 53 lines of complex transaction_schema and info_schema dictionaries
  • Replaced manual field mapping and validation logic with declarative Pydantic models

Added robust Pydantic models:

  • Transaction model with 20+ fields mapped from API column format (column0-column27)
  • Info model for account information with proper type conversion
  • Custom validators handle complex API response format with nested {name, value, id} objects
  • Automatic type conversion for both float and Decimal modes

Simplified parsing methods:

  • _parse_info() and _parse_transactions() now use clean model validation
  • Preserved all existing functionality including data refinements and transformations
  • Maintained backward compatibility - public interface unchanged

Benefits

  • Type safety: Automatic validation and type conversion via Pydantic
  • Maintainability: Clear model definitions replace complex dictionaries
  • Robustness: Better error handling and validation
  • Documentation: Models serve as living documentation of data structures
  • Code reduction: Eliminated complex custom parsing logic

Example

Before (custom schema):

self.transaction_schema = {
    "column0": ("date", coerce_date),
    "column1": ("amount", self.float_type),
    # ... 20+ more fields
}
# Complex parsing logic in _parse_transactions()

After (Pydantic model):

class Transaction(BaseModel):
    date: date | None = Field(None, alias="column0")
    amount: float | Decimal | None = Field(None, alias="column1")
    # ... clean field definitions with validators

The implementation includes conditional import fallback for environments where Pydantic isn't installed yet, ensuring smooth deployment.

Fixes #32.

[!WARNING]

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • astral.sh
    • Triggering command: curl -LsSf REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Jul 25 '25 20:07 Copilot