Skip to content

Commit 936a67b

Browse files
committed
fix: require both control digits for 12-digit INN
Nested ternary precedence skipped the first control when both were <=9, so invalid numbers like 000000000018 were accepted. Compare both checks via % 10.
1 parent 70de324 commit 936a67b

2 files changed

Lines changed: 8 additions & 12 deletions

File tree

src/validators/i18n/ru.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,17 @@ def ru_inn(value: str):
3939
if len(digits) == 10:
4040
weight_coefs = [2, 4, 10, 3, 5, 9, 4, 6, 8, 0]
4141
control_number = sum([d * w for d, w in zip(digits, weight_coefs)]) % 11
42-
return (
43-
(control_number % 10) == digits[-1]
44-
if control_number > 9
45-
else control_number == digits[-1]
46-
)
42+
return control_number % 10 == digits[-1]
4743
# person
4844
elif len(digits) == 12:
4945
weight_coefs1 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0, 0]
5046
control_number1 = sum([d * w for d, w in zip(digits, weight_coefs1)]) % 11
5147
weight_coefs2 = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0]
5248
control_number2 = sum([d * w for d, w in zip(digits, weight_coefs2)]) % 11
49+
# Both controls must match (% 10 maps 10 -> 0 per INN rules).
5350
return (
54-
(control_number1 % 10) == digits[-2]
55-
if control_number1 > 9
56-
else control_number1 == digits[-2] and (control_number2 % 10) == digits[-1]
57-
if control_number2 > 9
58-
else control_number2 == digits[-1]
51+
control_number1 % 10 == digits[-2]
52+
and control_number2 % 10 == digits[-1]
5953
)
6054
else:
6155
return False

tests/i18n/test_ru.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ def test_returns_true_on_valid_ru_inn(value: str):
4141
("7707012149",),
4242
("140700989886",),
4343
("774334078054",),
44+
("000000000018",),
45+
("000000000400",),
4446
],
4547
)
46-
def test_returns_false_on_valid_ru_inn(value: str):
47-
"""Test returns true on valid russian individual tax number."""
48+
def test_returns_false_on_invalid_ru_inn(value: str):
49+
"""Test returns ValidationError on invalid russian individual tax number."""
4850
assert isinstance(ru_inn(value), ValidationError)

0 commit comments

Comments
 (0)