From 10c27870b9b76006295d048e90f2903950486b2a Mon Sep 17 00:00:00 2001 From: Jovial Joe Jayarson Date: Wed, 3 Apr 2024 15:00:55 +0530 Subject: [PATCH] feat: adds indian aadhar and pan validator > co-authored by: @10kartik --- docs/api/i18n.md | 2 ++ docs/api/i18n.rst | 2 ++ src/validators/__init__.py | 15 ++++++++++- src/validators/i18n/__init__.py | 7 ++--- src/validators/i18n/ind.py | 47 +++++++++++++++++++++++++++++++++ tests/i18n/test_ind.py | 32 ++++++++++++++++++++++ 6 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 src/validators/i18n/ind.py create mode 100644 tests/i18n/test_ind.py diff --git a/docs/api/i18n.md b/docs/api/i18n.md index 55e77433..6999f33f 100644 --- a/docs/api/i18n.md +++ b/docs/api/i18n.md @@ -8,3 +8,5 @@ ::: validators.i18n.fi_ssn ::: validators.i18n.fr_department ::: validators.i18n.fr_ssn +::: validators.i18n.ind_aadhar +::: validators.i18n.ind_pan diff --git a/docs/api/i18n.rst b/docs/api/i18n.rst index 4a905e29..1284b302 100644 --- a/docs/api/i18n.rst +++ b/docs/api/i18n.rst @@ -10,3 +10,5 @@ i18n .. autofunction:: fi_ssn .. autofunction:: fr_department .. autofunction:: fr_ssn +.. autofunction:: ind_aadhar +.. autofunction:: ind_pan diff --git a/src/validators/__init__.py b/src/validators/__init__.py index 87e0c015..932098cd 100644 --- a/src/validators/__init__.py +++ b/src/validators/__init__.py @@ -11,7 +11,18 @@ from .encoding import base58, base64 from .hashes import md5, sha1, sha224, sha256, sha512 from .hostname import hostname -from .i18n import es_cif, es_doi, es_nie, es_nif, fi_business_id, fi_ssn, fr_department, fr_ssn +from .i18n import ( + es_cif, + es_doi, + es_nie, + es_nif, + fi_business_id, + fi_ssn, + fr_department, + fr_ssn, + ind_aadhar, + ind_pan, +) from .iban import iban from .ip_address import ipv4, ipv6 from .length import length @@ -67,6 +78,8 @@ "fi_ssn", "fr_department", "fr_ssn", + "ind_aadhar", + "ind_pan", # ... "iban", # ip_addresses diff --git a/src/validators/i18n/__init__.py b/src/validators/i18n/__init__.py index 45f45704..58385e0b 100644 --- a/src/validators/i18n/__init__.py +++ b/src/validators/i18n/__init__.py @@ -1,11 +1,10 @@ -"""Country.""" - -# isort: skip_file +"""i18n.""" # local from .es import es_cif, es_doi, es_nie, es_nif from .fi import fi_business_id, fi_ssn from .fr import fr_department, fr_ssn +from .ind import ind_aadhar, ind_pan __all__ = ( "fi_business_id", @@ -16,4 +15,6 @@ "es_nif", "fr_department", "fr_ssn", + "ind_aadhar", + "ind_pan", ) diff --git a/src/validators/i18n/ind.py b/src/validators/i18n/ind.py new file mode 100644 index 00000000..625e3012 --- /dev/null +++ b/src/validators/i18n/ind.py @@ -0,0 +1,47 @@ +"""India.""" + +# standard +import re + +# local +from validators.utils import validator + + +@validator +def ind_aadhar(value: str): + """Validate an indian aadhar card number. + + Examples: + >>> ind_aadhar('3675 9834 6015') + True + >>> ind_aadhar('3675 ABVC 2133') + ValidationFailure(func=aadhar, args={'value': '3675 ABVC 2133'}) + + Args: + value: Aadhar card number string to validate. + + Returns: + (Literal[True]): If `value` is a valid aadhar card number. + (ValidationError): If `value` is an invalid aadhar card number. + """ + return re.match(r"^[2-9]{1}\d{3}\s\d{4}\s\d{4}$", value) + + +@validator +def ind_pan(value: str): + """Validate a pan card number. + + Examples: + >>> ind_pan('ABCDE9999K') + True + >>> ind_pan('ABC5d7896B') + ValidationFailure(func=pan, args={'value': 'ABC5d7896B'}) + + Args: + value: PAN card number string to validate. + + Returns: + (Literal[True]): If `value` is a valid PAN card number. + (ValidationError): If `value` is an invalid PAN card number. + """ + return re.match(r"[A-Z]{5}\d{4}[A-Z]{1}", value) diff --git a/tests/i18n/test_ind.py b/tests/i18n/test_ind.py new file mode 100644 index 00000000..1164f131 --- /dev/null +++ b/tests/i18n/test_ind.py @@ -0,0 +1,32 @@ +"""Test Indian validators.""" + +# external +import pytest + +# local +from validators import ValidationError +from validators.i18n import ind_aadhar, ind_pan + + +@pytest.mark.parametrize("value", ["3675 9834 6012", "5046 3182 4299"]) +def test_returns_true_on_valid_ind_aadhar(value: str): + """Test returns true on valid ind aadhar.""" + assert ind_aadhar(value) + + +@pytest.mark.parametrize("value", ["3675 9834 6012 8", "417598346012", "3675 98AF 60#2"]) +def test_returns_failed_validation_on_invalid_ind_aadhar(value: str): + """Test returns failed validation on invalid ind aadhar.""" + assert isinstance(ind_aadhar(value), ValidationError) + + +@pytest.mark.parametrize("value", ["ABCDE9999K", "AAAPL1234C"]) +def test_returns_true_on_valid_ind_pan(value: str): + """Test returns true on valid ind pan.""" + assert ind_pan(value) + + +@pytest.mark.parametrize("value", ["ABC5d7896B", "417598346012", "AaaPL1234C"]) +def test_returns_failed_validation_on_invalid_ind_pan(value: str): + """Test returns failed validation on invalid ind pan.""" + assert isinstance(ind_pan(value), ValidationError)