Skip to content

Commit

Permalink
fix color check
Browse files Browse the repository at this point in the history
  • Loading branch information
elsholz committed Jan 13, 2024
1 parent 666b376 commit d6a9795
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions api/code/metroplanner_api/type_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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):
Expand Down

0 comments on commit d6a9795

Please sign in to comment.