Skip to content

Commit

Permalink
Add language code definitions and test
Browse files Browse the repository at this point in the history
* added literal with custom exception message
* tested all possibilities exhaustively
  • Loading branch information
pepe-rtmlab authored and “07pepe” committed Feb 24, 2024
1 parent f6dba1e commit e90492a
Show file tree
Hide file tree
Showing 2 changed files with 410 additions and 0 deletions.
205 changes: 205 additions & 0 deletions pydantic_extra_types/language_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
from typing import Annotated, Any
from typing import Literal

from pydantic import WrapValidator, ValidationInfo


def _not_iso_code(v: Any, next_: Any, ctx: ValidationInfo) -> Any:
try:
return next_(v, ctx)
except Exception as _:
raise ValueError(f"{v} is not a valid ISO 639-3 language code. "
f"See https://wikipedia.org/wiki/ISO_639-3.") from None


# language code definition as defined in ISO 639-3 https://wikipedia.org/wiki/ISO_639-3
# basically just defines list of literals https://docs.pydantic.dev/1.10/usage/types/#literal-type
LanguageCode = Annotated[
Literal[
'aar',
'abk',
'afr',
'aka',
'alb',
'amh',
'ara',
'arg',
'arm',
'asm',
'ava',
'ave',
'aym',
'aze',
'bak',
'bam',
'baq',
'bel',
'ben',
'bih',
'bis',
'bos',
'bre',
'bul',
'bur',
'cat',
'cha',
'che',
'chi',
'chu',
'chv',
'cor',
'cos',
'cre',
'cze',
'dan',
'div',
'dut',
'dzo',
'eng',
'epo',
'est',
'ewe',
'fao',
'fij',
'fin',
'fre',
'fry',
'ful',
'geo',
'ger',
'gla',
'gle',
'glg',
'glv',
'gre',
'grn',
'guj',
'hat',
'hau',
'heb',
'her',
'hin',
'hmo',
'hrv',
'hun',
'ibo',
'ice',
'ido',
'iii',
'iku',
'ile',
'ina',
'ind',
'ipk',
'ita',
'jav',
'jpn',
'kal',
'kan',
'kas',
'kau',
'kaz',
'khm',
'kik',
'kin',
'kir',
'kom',
'kon',
'kor',
'kua',
'kur',
'lao',
'lat',
'lav',
'lim',
'lin',
'lit',
'ltz',
'lub',
'lug',
'mac',
'mah',
'mal',
'mao',
'mar',
'may',
'mlg',
'mlt',
'mon',
'nau',
'nav',
'nbl',
'nde',
'ndo',
'nep',
'nno',
'nob',
'nor',
'nya',
'oci',
'oji',
'ori',
'orm',
'oss',
'pan',
'per',
'pli',
'pol',
'por',
'pus',
'que',
'roh',
'rum',
'run',
'rus',
'sag',
'san',
'sin',
'slo',
'slv',
'sme',
'smo',
'sna',
'snd',
'som',
'sot',
'spa',
'srd',
'srp',
'ssw',
'sun',
'swa',
'swe',
'tah',
'tam',
'tat',
'tel',
'tgk',
'tgl',
'tha',
'tib',
'tir',
'ton',
'tsn',
'tso',
'tuk',
'tur',
'twi',
'uig',
'ukr',
'urd',
'uzb',
'ven',
'vie',
'vol',
'wel',
'wln',
'wol',
'xho',
'yid',
'yor',
'zha',
'zul',
],
WrapValidator(_not_iso_code),
]
Loading

0 comments on commit e90492a

Please sign in to comment.