Skip to content

Commit 06ac008

Browse files
fix: add validator
1 parent 42957be commit 06ac008

File tree

1 file changed

+19
-3
lines changed
  • packages/models-library/src/models_library/api_schemas_webserver

1 file changed

+19
-3
lines changed

packages/models-library/src/models_library/api_schemas_webserver/storage.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pathlib import Path
2-
from typing import Annotated
2+
from typing import Annotated, Final
33

4-
from pydantic import BaseModel, Field
4+
from pydantic import BaseModel, BeforeValidator, Field
55

66
from ..api_schemas_storage.storage_schemas import (
77
DEFAULT_NUMBER_OF_PATHS_PER_PAGE,
@@ -11,6 +11,7 @@
1111
from ..rest_pagination import CursorQueryParameters
1212
from ._base import InputSchema
1313

14+
MIN_NON_WILDCARD_CHARS: Final[int] = 3
1415

1516
class StorageLocationPathParams(BaseModel):
1617
location_id: LocationID
@@ -44,5 +45,20 @@ class DataExportPost(InputSchema):
4445
paths: list[PathToExport]
4546

4647

48+
def validate_pattern_has_enough_characters(v: str) -> str:
49+
non_wildcard_chars = len([c for c in v if c not in ("*", "?")])
50+
51+
if non_wildcard_chars < MIN_NON_WILDCARD_CHARS:
52+
msg = f"Name pattern must contain at least {MIN_NON_WILDCARD_CHARS} non-wildcard characters (not * or ?), got {non_wildcard_chars}"
53+
raise ValueError(msg)
54+
return v
55+
56+
4757
class SearchBodyParams(InputSchema):
48-
name_pattern: str
58+
name_pattern: Annotated[
59+
str,
60+
BeforeValidator(validate_pattern_has_enough_characters),
61+
Field(
62+
description="Name pattern with wildcard support (* and ?). Minimum of {MIN_NON_WILDCARD_CHARS} non-wildcard characters required.",
63+
)
64+
]

0 commit comments

Comments
 (0)