|
| 1 | +import re |
| 2 | +import logging |
| 3 | + |
| 4 | +logger = logging.getLogger(__name__) |
| 5 | + |
| 6 | +def validate_amount(amount): |
| 7 | + """Validate that the amount is a positive number.""" |
| 8 | + if not isinstance(amount, (int, float, Decimal)) or amount <= 0: |
| 9 | + logger.error("Invalid amount: %s", amount) |
| 10 | + raise ValueError("Amount must be a positive number.") |
| 11 | + logger.debug("Validated amount: %s", amount) |
| 12 | + return True |
| 13 | + |
| 14 | +def validate_currency(currency): |
| 15 | + """Validate that the currency is a valid ISO 4217 code.""" |
| 16 | + if not isinstance(currency, str) or len(currency) != 3 or not currency.isalpha(): |
| 17 | + logger.error("Invalid currency code: %s", currency) |
| 18 | + raise ValueError("Currency must be a 3-letter ISO code.") |
| 19 | + logger.debug("Validated currency: %s", currency) |
| 20 | + return currency.upper() |
| 21 | + |
| 22 | +def validate_email(email): |
| 23 | + """Validate that the email address is in a valid format.""" |
| 24 | + email_regex = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' |
| 25 | + if not re.match(email_regex, email): |
| 26 | + logger.error("Invalid email address format: %s", email) |
| 27 | + raise ValueError("Invalid email address format.") |
| 28 | + logger.debug("Validated email: %s", email) |
| 29 | + return email |
| 30 | + |
| 31 | +def validate_payment_method(method): |
| 32 | + """Validate that the payment method is supported.""" |
| 33 | + supported_methods = ['coinbase', 'bitpay'] |
| 34 | + if method not in supported_methods: |
| 35 | + logger.error("Unsupported payment method: %s", method) |
| 36 | + raise ValueError(f"Unsupported payment method: {method}. Supported methods: {supported_methods}") |
| 37 | + logger.debug("Validated payment method: %s", method) |
| 38 | + return method |
| 39 | + |
| 40 | +def validate_transaction_data(transaction_data): |
| 41 | + """Validate transaction data structure.""" |
| 42 | + required_fields = ['transaction_id', 'amount', 'currency', 'status'] |
| 43 | + for field in required_fields: |
| 44 | + if field not in transaction_data: |
| 45 | + logger.error("Missing required field in transaction data: %s", field) |
| 46 | + raise ValueError(f"Missing required field in transaction data: {field}") |
| 47 | + logger.debug("Transaction data validated: %s", transaction_data) |
0 commit comments