Map Language for pydantic

This commit is contained in:
Zoe Roux 2025-05-10 15:37:04 +02:00
parent de199eeec4
commit 965cad76bb
No known key found for this signature in database
6 changed files with 50 additions and 21 deletions

View File

@ -1,8 +1,6 @@
from __future__ import annotations
from langcodes import Language
from ..utils import Model
from ..utils import Language, Model
from .genre import Genre
from .metadataid import MetadataId

View File

@ -3,9 +3,7 @@ from __future__ import annotations
from datetime import date
from typing import Literal
from langcodes import Language
from ..utils import Model
from ..utils import Language, Model
from .metadataid import EpisodeId, MetadataId

View File

@ -3,9 +3,7 @@ from __future__ import annotations
from datetime import date
from enum import StrEnum
from langcodes import Language
from ..utils import Model
from ..utils import Language, Model
from .collection import Collection
from .genre import Genre
from .metadataid import MetadataId

View File

@ -2,9 +2,7 @@ from __future__ import annotations
from datetime import date
from langcodes import Language
from ..utils import Model
from ..utils import Language, Model
from .metadataid import SeasonId

View File

@ -3,9 +3,7 @@ from __future__ import annotations
from datetime import date
from enum import StrEnum
from langcodes import Language
from ..utils import Model
from ..utils import Language, Model
from .collection import Collection
from .entry import Entry
from .extra import Extra

View File

@ -1,15 +1,16 @@
from langcodes import Language
from pydantic import AliasGenerator, BaseModel, ConfigDict
from typing import Annotated, Any, Callable
from langcodes import Language as BaseLanguage
from pydantic import AliasGenerator, BaseModel, ConfigDict, GetJsonSchemaHandler
from pydantic.alias_generators import to_camel
def normalize_lang(lang: str) -> str:
return str(Language.get(lang))
from pydantic.json_schema import JsonSchemaValue
from pydantic_core import core_schema
def to_slug(title: str) -> str:
return title
def clean(val: str) -> str | None:
return val or None
@ -21,3 +22,41 @@ class Model(BaseModel):
serialization_alias=lambda x: to_camel(x[:-1] if x[-1] == "_" else x),
),
)
class _LanguagePydanticAnnotation:
@classmethod
def __get_pydantic_core_schema__(
cls,
_source_type: Any,
_handler: Callable[[Any], core_schema.CoreSchema],
) -> core_schema.CoreSchema:
def validate_from_str(value: str) -> BaseLanguage:
return BaseLanguage.get(value)
from_str_schema = core_schema.chain_schema(
[
core_schema.str_schema(),
core_schema.no_info_plain_validator_function(validate_from_str),
]
)
return core_schema.json_or_python_schema(
json_schema=from_str_schema,
python_schema=core_schema.union_schema(
[
core_schema.is_instance_schema(BaseLanguage),
from_str_schema,
]
),
serialization=core_schema.to_string_ser_schema(),
)
@classmethod
def __get_pydantic_json_schema__(
cls, _core_schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler
) -> JsonSchemaValue:
return handler(core_schema.str_schema())
Language = Annotated[BaseLanguage, _LanguagePydanticAnnotation]