Spain National Id is a string composed by 8 numbers plus a letter The letter in fact is not part of the ID, it acts as a validator, checking you didn't do a mistake when entering it on a system or are giving a fake one. https://en.wikipedia.org/wiki/Documento_Nacional_de_Identi
(spanish_id: str)
| 3 | |
| 4 | |
| 5 | def is_spain_national_id(spanish_id: str) -> bool: |
| 6 | """ |
| 7 | Spain National Id is a string composed by 8 numbers plus a letter |
| 8 | The letter in fact is not part of the ID, it acts as a validator, |
| 9 | checking you didn't do a mistake when entering it on a system or |
| 10 | are giving a fake one. |
| 11 | |
| 12 | https://en.wikipedia.org/wiki/Documento_Nacional_de_Identidad_(Spain)#Number |
| 13 | |
| 14 | >>> is_spain_national_id("12345678Z") |
| 15 | True |
| 16 | >>> is_spain_national_id("12345678z") # It is case-insensitive |
| 17 | True |
| 18 | >>> is_spain_national_id("12345678x") |
| 19 | False |
| 20 | >>> is_spain_national_id("12345678I") |
| 21 | False |
| 22 | >>> is_spain_national_id("12345678-Z") # Some systems add a dash |
| 23 | True |
| 24 | >>> is_spain_national_id("12345678") |
| 25 | Traceback (most recent call last): |
| 26 | ... |
| 27 | ValueError: Input must be a string of 8 numbers plus letter |
| 28 | >>> is_spain_national_id("123456709") |
| 29 | Traceback (most recent call last): |
| 30 | ... |
| 31 | ValueError: Input must be a string of 8 numbers plus letter |
| 32 | >>> is_spain_national_id("1234567--Z") |
| 33 | Traceback (most recent call last): |
| 34 | ... |
| 35 | ValueError: Input must be a string of 8 numbers plus letter |
| 36 | >>> is_spain_national_id("1234Z") |
| 37 | Traceback (most recent call last): |
| 38 | ... |
| 39 | ValueError: Input must be a string of 8 numbers plus letter |
| 40 | >>> is_spain_national_id("1234ZzZZ") |
| 41 | Traceback (most recent call last): |
| 42 | ... |
| 43 | ValueError: Input must be a string of 8 numbers plus letter |
| 44 | >>> is_spain_national_id(12345678) |
| 45 | Traceback (most recent call last): |
| 46 | ... |
| 47 | TypeError: Expected string as input, found int |
| 48 | """ |
| 49 | |
| 50 | if not isinstance(spanish_id, str): |
| 51 | msg = f"Expected string as input, found {type(spanish_id).__name__}" |
| 52 | raise TypeError(msg) |
| 53 | |
| 54 | spanish_id_clean = spanish_id.replace("-", "").upper() |
| 55 | if len(spanish_id_clean) != 9: |
| 56 | raise ValueError(NUMBERS_PLUS_LETTER) |
| 57 | |
| 58 | try: |
| 59 | number = int(spanish_id_clean[0:8]) |
| 60 | letter = spanish_id_clean[8] |
| 61 | except ValueError as ex: |
| 62 | raise ValueError(NUMBERS_PLUS_LETTER) from ex |
nothing calls this directly
no outgoing calls
no test coverage detected