Skip to content

Commit

Permalink
ValidationError added
Browse files Browse the repository at this point in the history
  • Loading branch information
nvmbrasserie committed Nov 20, 2024
1 parent 70a9d89 commit 3ac38cb
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions stdnum/ru/ogrn.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,24 @@
import re
from typing import Optional

from stdnum.exceptions import ValidationError


OGRN_RE = re.compile(r'\b(\d{13}|\d{15})\b')

# Valid set of federal subject codes.
VALID_FEDERAL_SUBJECT_CODES = set(range(1, 80)) | {83, 86, 87, 89, 91, 92, 99}


def is_valid(text: str) -> bool:
def is_valid(text: str):
"""Determine if the given string is a valid OGRN."""
if OGRN_RE.match(text) is None:
return False
raise ValidationError("Invalid length for OGRN.")
if text[0] == '0':
return False
raise ValidationError("Invalid first digit for OGRN.")
federal_subject_code = int(text[3:5])
if federal_subject_code not in VALID_FEDERAL_SUBJECT_CODES:
return False
raise ValidationError("Invalid check digit for OGRN.")
control_digit = int(text[-1])
return control_digit == calculate_control_digit(text)

Expand Down

0 comments on commit 3ac38cb

Please sign in to comment.