clean pydatic passport

This commit is contained in:
robinrolle
2025-04-12 16:30:16 +02:00
parent c5f77affaa
commit f7383863ec

View File

@ -5,22 +5,41 @@ from pydantic import BaseModel, ConfigDict, Field, model_validator
class FromPassport(BaseModel):
"""
Fields which can be extracted from description.txt
Extracted fields from a scanned passport text.
These fields represent key identity and document metadata typically found in machine-readable passports.
"""
model_config = ConfigDict(validate_assignment=True, str_strip_whitespace=True)
country: str = Field(..., min_length=3, max_length=3) # ISO 3166-1 alpha-3
passport_number: str = Field(..., min_length=9, max_length=9, pattern=r"^[A-Z0-9]{9}$")
model_config = ConfigDict(validate_assignment=False, str_strip_whitespace=True)
surname: str = Field(..., min_length=1)
given_names: str = Field(..., min_length=1)
country: str = Field(
...,
min_length=3,
max_length=3,
description="The country issuing the passport, as a 3-letter ISO 3166-1 alpha-3 code (e.g., 'CHE' for Switzerland)."
)
birth_date: date
citizenship: str = Field(..., min_length=2)
sex: Literal["M", "F"]
passport_number: str = Field(
...,
min_length=9,
max_length=9,
pattern=r"^[A-Z0-9]{9}$",
description="The passport number, exactly 9 alphanumeric uppercase characters."
)
issue_date: date
expiry_date: date
surname: str = Field(
...,
min_length=1,
description="The surname (family name) of the passport holder."
)
given_names: str = Field(
...,
min_length=1,
description="The given names (first and middle names) of the passport holder."
)
@model_validator(mode='after')
def check_expiry_date_after_issue_date(self) -> Self:
@ -28,6 +47,28 @@ class FromPassport(BaseModel):
raise ValueError(f'Expiry date is not after issue date')
return self
signature_present: bool
birth_date: date = Field(
...,
description="Date of birth of the passport holder in ISO format (YYYY-MM-DD)."
)
machine_readable_zone: str = Field(..., min_length=44)
citizenship: str = Field(
...,
min_length=2,
description="The nationality or citizenship of the passport holder, preferably as a country name or ISO code."
)
sex: Literal["M", "F"] = Field(
...,
description="Sex of the passport holder: 'M' for male, 'F' for female."
)
issue_date: date = Field(
...,
description="Date when the passport was issued, in ISO format (YYYY-MM-DD)."
)
expiry_date: date = Field(
...,
description="Date when the passport expires, in ISO format (YYYY-MM-DD)."
)