Skip to content

Commit 05bdb76

Browse files
fix: move validator to common
1 parent 57ba337 commit 05bdb76

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

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

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

4-
from pydantic import BaseModel, BeforeValidator, Field
4+
from models_library.utils.common_validators import (
5+
MIN_NON_WILDCARD_CHARS,
6+
WILDCARD_CHARS,
7+
ensure_pattern_has_enough_characters,
8+
)
9+
from pydantic import BaseModel, Field
510

611
from ..api_schemas_storage.storage_schemas import (
712
DEFAULT_NUMBER_OF_PATHS_PER_PAGE,
@@ -11,8 +16,6 @@
1116
from ..rest_pagination import CursorQueryParameters
1217
from ._base import InputSchema
1318

14-
MIN_NON_WILDCARD_CHARS: Final[int] = 3
15-
1619

1720
class StorageLocationPathParams(BaseModel):
1821
location_id: LocationID
@@ -46,20 +49,11 @@ class DataExportPost(InputSchema):
4649
paths: list[PathToExport]
4750

4851

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

packages/models-library/src/models_library/utils/common_validators.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ class MyModel(BaseModel):
1818
import enum
1919
import functools
2020
import operator
21-
from typing import Any
21+
from typing import Any, Final
2222

2323
from common_library.json_serialization import json_loads
2424
from orjson import JSONDecodeError
2525
from pydantic import BaseModel, BeforeValidator
2626
from pydantic.alias_generators import to_camel
2727

28+
WILDCARD_CHARS: Final[set[str]] = {"*", "?"}
29+
MIN_NON_WILDCARD_CHARS: Final[int] = 3
30+
2831

2932
def trim_string_before(max_length: int) -> BeforeValidator:
3033
def _trim(value: str):
@@ -145,3 +148,21 @@ def to_camel_recursive(data: dict[str, Any]) -> dict[str, Any]:
145148
else:
146149
new_dict[new_key] = value
147150
return new_dict
151+
152+
153+
def ensure_pattern_has_enough_characters(
154+
min_non_wildcard_chars: int = MIN_NON_WILDCARD_CHARS,
155+
wildcard_chars: set[str] | None = None,
156+
) -> BeforeValidator:
157+
if wildcard_chars is None:
158+
wildcard_chars = WILDCARD_CHARS
159+
160+
def _validator(value):
161+
non_wildcard_chars = len([c for c in value if c not in wildcard_chars])
162+
163+
if non_wildcard_chars < min_non_wildcard_chars:
164+
msg = f"Pattern {value} must contain at least {min_non_wildcard_chars} non-wildcard characters, got {non_wildcard_chars}"
165+
raise ValueError(msg)
166+
return value
167+
168+
return BeforeValidator(_validator)

0 commit comments

Comments
 (0)