arthurdejong / python-stdnum

A Python library to provide functions to handle, parse and validate standard numbers.
https://arthurdejong.org/python-stdnum/
GNU Lesser General Public License v2.1
495 stars 205 forks source link

Norwegian orgnr validates invalid org numbers. #275

Open LPAiken opened 2 years ago

LPAiken commented 2 years ago

All norwegian org numbers must be between 800_000_000 and 999_999_999, and your library validates org numbers like "100 000 008". Perhaps a better validation function would be, e.g.

def is_valid(orgnr):
    if isinstance(orgnr, str):
        orgnr = int(orgnr.replace(" ", ""))   # Strip whitespace
    elif not isinstance(orgnr, int):
        raise TypeError("orgnr must be a str or int.")
    if not 800_000_000 <= orgnr < 1_000_000_000:
        return False
    # Fast way to calculate the checksum
    weighted_sum = 0
    for weight in [1, 2, 3, 4, 5, 6, 7, 2, 3]:
        orgnr, remainder = divmod(orgnr, 10)
        weighted_sum += weight * remainder
    return not weighted_sum % 11
arthurdejong commented 2 years ago

Hi @LPAiken,

Thanks for reporting this. Do you have a reference somewhere to this extra check? It doesn't seem to be mentioned on https://no.wikipedia.org/wiki/Organisasjonsnummer, https://nn.wikipedia.org/wiki/Organisasjonsnummer or https://www.brreg.no/om-oss/oppgavene-vare/alle-registrene-vare/om-enhetsregisteret/organisasjonsnummeret/.