-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add language code definitions and test
* added literal with custom exception message * tested all possibilities exhaustively
- Loading branch information
1 parent
f6dba1e
commit e90492a
Showing
2 changed files
with
410 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
] |
Oops, something went wrong.