diff --git a/stdnum/ro/cf.py b/stdnum/ro/cf.py index 918df30a..80b72338 100644 --- a/stdnum/ro/cf.py +++ b/stdnum/ro/cf.py @@ -20,8 +20,12 @@ The Romanian CF is used for VAT purposes and can be from 2 to 10 digits long. ->>> validate('RO 185 472 90') # VAT CUI/CIF -'RO18547290' +>>> validate('RO 185 472 90') # VAT CUI/CIF (RO prefix is stripped) +'18547290' +>>> validate('RO RO 16621241') +Traceback (most recent call last): + ... +InvalidFormat: ... >>> validate('185 472 90') # non-VAT CUI/CIF '18547290' >>> validate('1630615123457') # CNP @@ -32,13 +36,17 @@ from stdnum.exceptions import * from stdnum.ro import cnp, cui -from stdnum.util import clean +from stdnum.util import clean, isdigits def compact(number: str) -> str: """Convert the number to the minimal representation. This strips the - number of any valid separators and removes surrounding whitespace.""" - return clean(number, ' -').upper().strip() + number of any valid separators and removes surrounding whitespace. The + optional RO country prefix is removed to return the domestic form.""" + number = clean(number, ' -').upper().strip() + if number.startswith('RO'): + number = number[2:] + return number # for backwards compatibility @@ -49,13 +57,12 @@ def validate(number: str) -> str: """Check if the number is a valid VAT number. This checks the length, formatting and check digit.""" number = compact(number) - cnumber = number - if cnumber.startswith('RO'): - cnumber = cnumber[2:] - if len(cnumber) == 13: + if not isdigits(number): + raise InvalidFormat() + if len(number) == 13: # apparently a CNP can also be used (however, not all sources agree) - cnp.validate(cnumber) - elif 2 <= len(cnumber) <= 10: + cnp.validate(number) + elif 2 <= len(number) <= 10: cui.validate(number) else: raise InvalidLength()