DEVALIDATION OF VALIDATION

This commit is contained in:
NoeBerdoz
2025-04-13 08:45:47 +02:00
parent 790cbb43ab
commit 54e794d670
5 changed files with 120 additions and 147 deletions

View File

@ -1,43 +1,56 @@
from typing import Literal, Optional, Self
from pydantic import BaseModel, ConfigDict, EmailStr, Field, model_validator
from pydantic_extra_types.phone_numbers import PhoneNumber
from typing import Optional # Removed Literal, Self
from pydantic import BaseModel, ConfigDict # Removed Field, model_validator
# Removed PhoneNumber import as it's not used and types are simplified
class FromAccount(BaseModel):
"""
Fields which can be extracted from account.pdf
Fields which can be extracted from account.pdf - All fields optional and simplified to string.
Validators removed for testing purposes.
"""
model_config = ConfigDict(validate_assignment=True, str_strip_whitespace=True)
# Details of the Account and Client
account_name: str = Field(min_length=1)
account_holder_name: str = Field(min_length=1)
account_holder_surname: str = Field(min_length=1)
account_name: Optional[str] = None
account_holder_name: Optional[str] = None
account_holder_surname: Optional[str] = None
@model_validator(mode='after')
def check_account_name_is_name_surname(self) -> Self:
combined = f"{self.account_holder_name} {self.account_holder_surname}"
if combined != self.account_name:
raise ValueError(f'Account name is not name + surname: {self.account_name} != {combined}')
return self
# --- Updated Validator 1 ---
# @model_validator(mode='after')
# def check_account_name_is_name_surname(self) -> Self:
# # Only validate if all relevant fields are present
# if self.account_holder_name is not None and \
# self.account_holder_surname is not None and \
# self.account_name is not None:
# combined = f"{self.account_holder_name} {self.account_holder_surname}"
# if combined != self.account_name:
# raise ValueError(f'Account name is not name + surname: {self.account_name} != {combined}')
# return self
passport_number: str = Field(min_length=5)
passport_number: Optional[str] = None # Removed Field constraint
reference_currency: Literal["CHF", "EUR", "USD", "Other"]
reference_currency: Optional[str] = None # Simplified from Literal
other_currency: Optional[str] = None
# Delivery of Communication
building_number: str = Field(min_length=1)
street_name: str = Field(min_length=1)
postal_code: str = Field(min_length=1)
city: str = Field(min_length=1)
country: str = Field(min_length=1)
building_number: Optional[str] = None
street_name: Optional[str] = None
postal_code: Optional[str] = None
city: Optional[str] = None
country: Optional[str] = None
# Application for e-banking
ebanking_name: str = Field(min_length=1)
@model_validator(mode='after')
def check_account_name_ebanking_name(self) -> Self:
if self.ebanking_name != self.account_name:
raise ValueError(f'Ebanking name is different from account name')
return self
phone_number: str = Field(..., min_length=8)
email: str = Field(min_length=5)
ebanking_name: Optional[str] = None
# --- Updated Validator 2 ---
# @model_validator(mode='after')
# def check_account_name_ebanking_name(self) -> Self:
# # Only validate if both fields are present
# if self.ebanking_name is not None and self.account_name is not None:
# if self.ebanking_name != self.account_name:
# raise ValueError(f'Ebanking name ({self.ebanking_name}) is different from account name ({self.account_name})')
# return self
# Kept as Optional[str], but you might consider Optional[PhoneNumber]
# if you want specific phone number validation using pydantic-extra-types
phone_number: Optional[str] = None # Removed Field constraint
email: Optional[str] = None # Removed Field constraint