From d6a979520fb84d72bcb0e81ce296b5bcf32d287f Mon Sep 17 00:00:00 2001 From: elsholz Date: Sun, 14 Jan 2024 00:52:46 +0100 Subject: [PATCH] fix color check --- api/code/metroplanner_api/type_definitions.py | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/api/code/metroplanner_api/type_definitions.py b/api/code/metroplanner_api/type_definitions.py index 9d705b8..6c81e72 100644 --- a/api/code/metroplanner_api/type_definitions.py +++ b/api/code/metroplanner_api/type_definitions.py @@ -41,8 +41,8 @@ class MissingValueBaseClass(BaseModel): LocalizedLongText = Dict[str, LongText] MaybeLocalizedShortText = Union[LocalizedShortText, ShortText] MaybeLocalizedLongText = Union[LocalizedLongText, LongText] +Point = pydantic.conlist(IntOrFloat, min_length=2, max_length=2) Identifier = Annotated[str, pydantic.StringConstraints(max_length=36, min_length=36)] -# ColorCSS = pydantic_extra_types.color.Color ColorCSS = Annotated[str, pydantic_extra_types.color.Color] ColorReference = Annotated[ str, @@ -54,9 +54,32 @@ class MissingValueBaseClass(BaseModel): ) ), ] -Color = Union[ColorReference, ColorCSS] -Point = pydantic.conlist(IntOrFloat, min_length=2, max_length=2) + +def check_color(v: str) -> str: + ColorCSS = Annotated[str, pydantic_extra_types.color.Color] + ColorReference = Annotated[ + str, + pydantic.StringConstraints( + pattern=( + r"(^(fore|back)ground$)" + r"|(^landscape::(((deep|shallow)?water)|border)$)" + r"|(^lines::\d{1,3}$)" + ) + ), + ] + + class ModelCheck(BaseModel): + color: Union[ColorCSS, ColorReference] + + try: + m = ModelCheck(color=v) + return v + except Exception as e: + raise pydantic.ValidationError() + + +Color = Annotated[str, pydantic.AfterValidator(check_color)] def MaybeMissing(t):