Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def validate_result(self, pattern_text: str) -> Optional[bool]:
except ValueError:
return False
individual_number = pattern_text[7:10]
control_character = pattern_text[-1]
# The control character is defined in upper case; the pattern is matched
# case-insensitively, so upper-case it before comparing or a valid code
# written with a lower-case control character would be rejected.
control_character = pattern_text[-1].upper()
valid_control_characters = "0123456789ABCDEFHJKLMNPRSTUVWXY"
number_to_check = int(date_part + individual_number)
return valid_control_characters[number_to_check % 31] == control_character
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def validate_result(self, pattern_text: str) -> Optional[bool]:
Only the part in text that was detected by the regex engine
:return: A bool indicating whether the validation was successful.
"""
# The prefix, entity-type and check letters are defined in upper case;
# the pattern is matched case-insensitively, so normalize before the
# checksum or a valid UEN written in lower case would be rejected.
pattern_text = pattern_text.upper()

if len(pattern_text) == 9:
# Checksum validation for UEN format A
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ def entities():
),
# Valid HETU with the canonical "-" century separator.
("131052-308T", 1, ((0, 11),)),
# Regression: a valid code whose control character is written in lower
# case must still be detected. The pattern matches case-insensitively,
# so the control character is upper-cased before the checksum.
("131052-308t", 1, ((0, 11),)),
("020504a902e", 1, ((0, 11),)),
# invalid Personal Identity Codes scores
("111111-111A", 0, ()),
("111111+110G", 0, ()),
Expand Down
4 changes: 4 additions & 0 deletions presidio-analyzer/tests/test_sg_uen_recognizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def entities():
("53125226D 201434292D S57TU0392K", 3, [(0, 9), (10, 20), (21, 31)],),
# Test with valid UEN in a sentence
("UEN 53125226D was processed", 1, [(4, 13)],),
# Lower-case UENs must still validate; the prefix, entity-type and
# check letters are upper-cased before the checksum. Format A and C:
("53125226d", 1, [(0, 9)],),
("t16rf0037c", 1, [(0, 10)],),

## No match
# Test with invalid length
Expand Down
Loading