diff --git a/stdnum/iso6346.py b/stdnum/iso6346.py index c7edf347..fbaf4c6d 100644 --- a/stdnum/iso6346.py +++ b/stdnum/iso6346.py @@ -48,7 +48,7 @@ from stdnum.util import clean -_iso6346_re = re.compile(r'^\w{3}(U|J|Z|R)\d{7}$') +_iso6346_re = re.compile(r'^[A-Z]{3}(U|J|Z|R)[0-9]{7}$') def compact(number: str) -> str: diff --git a/stdnum/pt/cc.py b/stdnum/pt/cc.py index f98f40eb..a0ef4a09 100644 --- a/stdnum/pt/cc.py +++ b/stdnum/pt/cc.py @@ -48,7 +48,7 @@ from stdnum.util import clean -_cc_re = re.compile(r'^\d*[A-Z0-9]{2}\d$') +_cc_re = re.compile(r'^[0-9]*[A-Z0-9]{2}[0-9]$') def compact(number: str) -> str: diff --git a/tests/test_iso6346.doctest b/tests/test_iso6346.doctest index d8758167..feee81a5 100644 --- a/tests/test_iso6346.doctest +++ b/tests/test_iso6346.doctest @@ -57,3 +57,18 @@ False Traceback (most recent call last): ... InvalidFormat: ... + + +Letters and digits outside of the ASCII range are not valid and should be +rejected with a validation error instead of crashing. + +>>> iso6346.is_valid('CSQU٣054383') +False +>>> iso6346.is_valid('CSÉU3054383') +False +>>> iso6346.is_valid('CS_U3054383') +False +>>> iso6346.validate('CSQU٣054383') +Traceback (most recent call last): + ... +InvalidFormat: ... diff --git a/tests/test_pt_cc.doctest b/tests/test_pt_cc.doctest index 65e4f060..5c26bce6 100644 --- a/tests/test_pt_cc.doctest +++ b/tests/test_pt_cc.doctest @@ -34,3 +34,16 @@ These have been found online and should all be valid numbers. ... ''' >>> [x for x in numbers.splitlines() if x and not cc.is_valid(x)] [] + + +Non-ASCII digits are not valid and should be rejected with a validation +error instead of crashing. + +>>> cc.is_valid('00000000 ٠ ZZ4') +False +>>> cc.is_valid('٠٠٠٠٠٠٠٠٠ZZ4') +False +>>> cc.validate('00000000 ٠ ZZ4') +Traceback (most recent call last): + ... +InvalidFormat: ...