From 652d1445a83e8817b3ccbd08dbb7bcd82464d890 Mon Sep 17 00:00:00 2001 From: David Kilias Date: Sat, 7 Nov 2020 19:53:47 +0100 Subject: [PATCH] add function to validate money fields --- validators/test_validators.py | 11 ++++++++++- validators/validators.py | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/validators/test_validators.py b/validators/test_validators.py index 16d3a9a..3094007 100644 --- a/validators/test_validators.py +++ b/validators/test_validators.py @@ -1,4 +1,4 @@ -from validators import mandatory, empty, iso_date, valid_iban, valid_bic +from validators import mandatory, empty, iso_date, valid_iban, valid_bic, valid_money_amount def test_mandatory(): @@ -48,3 +48,12 @@ def test_valid_bic(): assert valid_bic("PBNKDEFFXXX".split()) == False +def test_valid_money_amount(): + assert valid_money_amount(None) == False + assert valid_money_amount("") == False + assert valid_money_amount("0,00") == False + assert valid_money_amount("13,37") == False + assert valid_money_amount("0") == True + assert valid_money_amount("0.00") == True + assert valid_money_amount("13") == True + assert valid_money_amount("13.37") == True \ No newline at end of file diff --git a/validators/validators.py b/validators/validators.py index 9ff8f47..f0f33cc 100644 --- a/validators/validators.py +++ b/validators/validators.py @@ -42,3 +42,12 @@ def valid_bic(field: str) -> bool: return True except ValueError: return False + +def valid_money_amount(field: str) -> bool: + if not field: + return False + try: + float(field) + return True + except (ValueError, TypeError): + return False \ No newline at end of file