From d851405b6b17053f6daa48ef5b3b251b6dcdf8df Mon Sep 17 00:00:00 2001 From: David Kilias Date: Thu, 5 Nov 2020 17:04:02 +0100 Subject: [PATCH] add validator functions and tests --- validators/test_validators.py | 37 +++++++++++++++++++++++++++++++++++ validators/validators.py | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 validators/test_validators.py create mode 100644 validators/validators.py diff --git a/validators/test_validators.py b/validators/test_validators.py new file mode 100644 index 0000000..b959892 --- /dev/null +++ b/validators/test_validators.py @@ -0,0 +1,37 @@ +from validators import mandatory, empty, iso_date, valid_iban, valid_bic + + +def test_mandatory(): + assert mandatory("Foo") == True + assert mandatory("Foo Bar") == True + assert mandatory("1970-01-01") == True + assert mandatory("") == False + + +def test_empty(): + assert empty("") == True + assert empty(" ") == False + assert empty("Foo") == False + assert empty("1970-01-01") == False + + +def test_iso_date(): + assert iso_date("1970-01-01") == True + assert iso_date("1970-1-1") == True + assert iso_date("70-01-01") == False + assert iso_date("1970/01/01") == False + assert iso_date("1.1.1970") == False + assert iso_date("01.01.1970") == False + + +def test_valid_iban(): + assert valid_iban("DE89 3704 0044 0532 0130 00") == True + assert valid_iban("DX89 3704 0044 0532 0130 00") == False + assert valid_iban("DE99 3704 0044 0532 0130 00") == False + + +def test_valid_bic(): + assert valid_bic("PBNKDEFFXXX") == True + assert valid_bic("PBNKDXFFXXX") == False + assert valid_bic("PBNKDXFFXXXX") == False + assert valid_bic("PBN1DXFFXXX") == False diff --git a/validators/validators.py b/validators/validators.py new file mode 100644 index 0000000..aadab3e --- /dev/null +++ b/validators/validators.py @@ -0,0 +1,34 @@ +import datetime +from schwifty import IBAN, BIC + + +def mandatory(field: str) -> bool: + return bool(field) + + +def empty(field: str) -> bool: + return not bool(field) + + +def iso_date(field: str) -> bool: + try: + datetime.datetime.strptime(field, "%Y-%m-%d") + return True + except ValueError: + return False + + +def valid_iban(field: str) -> bool: + try: + IBAN(field) + return True + except ValueError: + return False + + +def valid_bic(field: str) -> bool: + try: + BIC(field) + return True + except ValueError: + return False