from typing import Annotated from pydantic.functional_validators import AfterValidator # Customize the validation rule def validate_payment_mthd(paymentMthd: str): possible_values = ['Credit card', 'Debit card', 'Digital wallet', 'Cash on delivery', 'Cryptocurrency'] if paymentMthd not in possible_values: raise ValueError(f"Invalid paymentMthd, payment type must be one of {possible_values}") return storage # Define the data model for a transaction record class TxnModel(BaseModel): orderID: int = Field(strict=True) customerID: int productID: list[int] paymentMthd: Annotated[str, AfterValidator(validate_payment_mthd)] totalAmt: Annotated[float, Field(strict=True, gt=0)] invoiceTime: datetime # Manually define a non-existent payment method sample_txn['paymentMthd'] = 'Bitcoin' try: # Validate the sample case against the schema TxnModel.model_validate(sample_txn) print("Validated successfully!") except ValidationError as exc: # Print error messages for any validation error print("Validation Error:") print(exc.errors()[0]['ctx']) # Output # Validation Error: # {'error': ValueError("Invalid paymentMthd, payment type must be one of ['Credit card', 'Debit card', 'Digital wallet', 'Cash on delivery', 'Cryptocurrency']")}