Skip to content

Commit c498dfb

Browse files
committed
gw: Add helper for validating model names
Validate model names, ensuring that they adhere to the Docker container name restrictions: * Must start with an alphanumeric character * Can contain alphanumeric characters, underscores, dots, and dashes Signed-off-by: Phoevos Kalemkeris <[email protected]>
1 parent 3787194 commit c498dfb

File tree

1 file changed

+28
-1
lines changed
  • cogstack_model_gateway/gateway/routers

1 file changed

+28
-1
lines changed
Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
import re
12
from typing import Annotated
23

3-
from fastapi import Header, Request
4+
from fastapi import Header, HTTPException, Path, Request
45

56
from cogstack_model_gateway.common.utils import parse_content_type_header
67

8+
MODEL_NAME_REGEX = r"^[a-zA-Z0-9][a-zA-Z0-9_.-]+$"
9+
VALID_MODEL_DESCRIPTION = (
10+
"The model name must start with an alphanumeric character and can only contain"
11+
" alphanumeric characters, underscores (_), dots (.), and dashes (-)."
12+
)
13+
714

815
def get_query_params(request: Request) -> dict[str, str]:
916
return dict(request.query_params)
@@ -12,3 +19,23 @@ def get_query_params(request: Request) -> dict[str, str]:
1219
def get_content_type(content_type: Annotated[str, Header()]) -> str:
1320
parsed_content_type, _ = parse_content_type_header(content_type)
1421
return parsed_content_type
22+
23+
24+
def validate_model_name(
25+
model_name: Annotated[
26+
str,
27+
Path(
28+
...,
29+
description=(
30+
"The name of the model to deploy (used as the container name)."
31+
f" {VALID_MODEL_DESCRIPTION}"
32+
),
33+
),
34+
],
35+
) -> str:
36+
if not re.match(MODEL_NAME_REGEX, model_name):
37+
raise HTTPException(
38+
status_code=400,
39+
detail=f"Invalid model name. {VALID_MODEL_DESCRIPTION}",
40+
)
41+
return model_name

0 commit comments

Comments
 (0)