Skip to content

Commit

Permalink
Create transaction_validation.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored May 10, 2024
1 parent 896f93f commit 978d3bc
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions transaction_processing/transaction_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import re

class TransactionValidation:
def __init__(self):
self.validation_rules = {
'account_number': {
'regex': r'^\d{10}$',
'message': 'Invalid account number format. Please enter a 10-digit account number.'
},
'amount': {
'min': 0,
'max': 1000000,
'message': 'Transaction amount must be between 0 and 1,000,000.'
}
}

def validate_transaction(self, transaction):
errors = []
for field, rules in self.validation_rules.items():
if field in transaction:
for rule in rules:
if rule == 'regex':
if not re.match(rules[rule], transaction[field]):
errors.append(rules['message'])
elif rule == 'min':
if float(transaction[field]) < rules[rule]:
errors.append(f'Transaction amount must be at least {rules[rule]}')
elif rule == 'max':
if float(transaction[field]) > rules[rule]:
errors.append(f'Transaction amount must not exceed {rules[rule]}')
return errors

0 comments on commit 978d3bc

Please sign in to comment.