-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #358 from yozachar/workshop
feat: adds indian aadhar and pan validator
- Loading branch information
Showing
6 changed files
with
101 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |