|
| 1 | +import re |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +from pydantic import GetCoreSchemaHandler |
| 5 | +from pydantic.json_schema import JsonSchemaValue |
| 6 | +from pydantic_core import core_schema |
| 7 | + |
| 8 | +# An OIN is always 20 characters: |
| 9 | +# prefix (8 digits) + mainnumber (8 or 9 alphanumeric) + suffix (4 or 3 zeros) |
| 10 | +OIN_PATTERN = re.compile(r"^\d{8}(?:[A-Za-z0-9]{8}0{4}|[A-Za-z0-9]{9}0{3})$") |
| 11 | + |
| 12 | + |
| 13 | +class Oin: |
| 14 | + """ |
| 15 | + Value object representing a Dutch OIN (Organisatie Identificatie nummer). |
| 16 | +
|
| 17 | + An OIN is exactly 20 positions long (matching the PKIoverheid certificate |
| 18 | + subject serial number field) and consists of three parts: |
| 19 | +
|
| 20 | + - Prefix (8 positions): digits identifying the issuing authority / register. |
| 21 | + - mainnumber (8 or 9 positions): identifying number from the register. |
| 22 | + Can be alphanumeric depending on the register used. |
| 23 | + When the source is a KvK number the mainnumber is 8 positions. |
| 24 | + - Suffix (3 or 4 positions, always zeros): |
| 25 | + - "0000" when the mainnumber is 8 positions. |
| 26 | + - "000" when the mainnumber is 9 positions. |
| 27 | +
|
| 28 | + https://gitdocumentatie.logius.nl/publicatie/dk/oin/3.0.0/#samenstelling-oin |
| 29 | + """ |
| 30 | + |
| 31 | + PREFIX_LENGTH = 8 |
| 32 | + TOTAL_LENGTH = 20 |
| 33 | + |
| 34 | + _LONG_SUFFIX = "0000" # used when mainnumber is 8 digits |
| 35 | + _SHORT_SUFFIX = "000" # used when mainnumber is 9 digits |
| 36 | + |
| 37 | + def __init__(self, value: Any) -> None: |
| 38 | + if isinstance(value, Oin): |
| 39 | + value = value.value |
| 40 | + |
| 41 | + if not isinstance(value, (int, str)): |
| 42 | + raise ValueError( |
| 43 | + f"OIN must be a string or integer, got {type(value).__name__}" |
| 44 | + ) |
| 45 | + |
| 46 | + if isinstance(value, int) and value < 0: |
| 47 | + raise ValueError("OIN must be a positive integer") |
| 48 | + |
| 49 | + str_value = str(value) |
| 50 | + |
| 51 | + if not OIN_PATTERN.match(str_value): |
| 52 | + raise ValueError( |
| 53 | + f"Invalid OIN {str_value!r}. " |
| 54 | + f"Expected {self.TOTAL_LENGTH} characters structured as " |
| 55 | + f"8 digit prefix + 8/9 alphanumeric mainnumber + 4/3 trailing zeros." |
| 56 | + ) |
| 57 | + |
| 58 | + self.prefix = str_value[: self.PREFIX_LENGTH] |
| 59 | + self.number = str_value[self.PREFIX_LENGTH :] |
| 60 | + |
| 61 | + @property |
| 62 | + def mainnumber(self) -> str: |
| 63 | + """ |
| 64 | + The identifying part of the OIN (8 or 9 characters, alphanumeric). |
| 65 | + 8 characters when the suffix is "0000"; 9 characters when the suffix is "000". |
| 66 | + """ |
| 67 | + if self.number.endswith(self._LONG_SUFFIX): |
| 68 | + return self.number[ |
| 69 | + : self.TOTAL_LENGTH - self.PREFIX_LENGTH - len(self._LONG_SUFFIX) |
| 70 | + ] |
| 71 | + return self.number[ |
| 72 | + : self.TOTAL_LENGTH - self.PREFIX_LENGTH - len(self._SHORT_SUFFIX) |
| 73 | + ] |
| 74 | + |
| 75 | + @property |
| 76 | + def suffix(self) -> str: |
| 77 | + """The trailing zero-padding ("0000" when mainnumber is 8 chars, "000" when 9 chars).""" |
| 78 | + if self.number.endswith(self._LONG_SUFFIX): |
| 79 | + return self._LONG_SUFFIX |
| 80 | + return self._SHORT_SUFFIX |
| 81 | + |
| 82 | + @property |
| 83 | + def value(self) -> str: |
| 84 | + return self.prefix + self.number |
| 85 | + |
| 86 | + def __str__(self) -> str: |
| 87 | + return self.value |
| 88 | + |
| 89 | + def __repr__(self) -> str: |
| 90 | + return f"Oin({self.prefix}, {self.number})" |
| 91 | + |
| 92 | + def __eq__(self, other: Any) -> bool: |
| 93 | + if isinstance(other, Oin): |
| 94 | + return self.value == other.value |
| 95 | + return False |
| 96 | + |
| 97 | + def __hash__(self) -> int: |
| 98 | + return hash(self.value) |
| 99 | + |
| 100 | + @classmethod |
| 101 | + def __get_pydantic_core_schema__( |
| 102 | + cls, _source_type: Any, _handler: GetCoreSchemaHandler |
| 103 | + ) -> core_schema.CoreSchema: |
| 104 | + return core_schema.no_info_plain_validator_function( |
| 105 | + cls._pydantic_validate, |
| 106 | + serialization=core_schema.to_string_ser_schema(), |
| 107 | + ) |
| 108 | + |
| 109 | + @classmethod |
| 110 | + def _pydantic_validate(cls, value: Any) -> "Oin": |
| 111 | + if isinstance(value, cls): |
| 112 | + return value |
| 113 | + try: |
| 114 | + return cls(value) |
| 115 | + except (ValueError, TypeError) as exc: |
| 116 | + raise ValueError(str(exc)) from exc |
| 117 | + |
| 118 | + @classmethod |
| 119 | + def __get_pydantic_json_schema__( |
| 120 | + cls, _schema: core_schema.CoreSchema, _handler: Any |
| 121 | + ) -> JsonSchemaValue: |
| 122 | + return { |
| 123 | + "type": "string", |
| 124 | + "pattern": r"^\d{8}(?:[A-Za-z0-9]{8}0{4}|[A-Za-z0-9]{9}0{3})$", |
| 125 | + "examples": ["00000099000000001000"], |
| 126 | + "description": ( |
| 127 | + "OIN (Organisatie Identificatie nummer): 20 characters structured as " |
| 128 | + "8-digit prefix + 8/9-character alphanumeric mainnumber + 4/3 trailing zeros (suffix)." |
| 129 | + ), |
| 130 | + } |
0 commit comments