Skip to content

Commit 95f2a4e

Browse files
committed
adds min lengh in patch
1 parent 772ef54 commit 95f2a4e

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def from_domain_model(
141141
class MyProfilePatch(InputSchemaWithoutCamelCase):
142142
first_name: FirstNameStr | None = None
143143
last_name: LastNameStr | None = None
144-
user_name: Annotated[IDStr | None, Field(alias="userName")] = None
144+
user_name: Annotated[IDStr | None, Field(alias="userName", min_length=4)] = None
145145

146146
privacy: MyProfilePrivacyPatch | None = None
147147

services/web/server/tests/unit/isolated/test_users_models.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from models_library.generics import Envelope
1919
from models_library.users import UserThirdPartyToken
2020
from models_library.utils.fastapi_encoders import jsonable_encoder
21-
from pydantic import BaseModel
21+
from pydantic import BaseModel, ValidationError
2222
from pytest_simcore.pydantic_models import (
2323
assert_validation_model,
2424
iter_model_examples_in_class,
@@ -185,3 +185,23 @@ def test_mapping_update_models_from_rest_to_db():
185185
"name": "foo1234",
186186
"privacy_hide_fullname": False,
187187
}
188+
189+
190+
def test_my_profile_patch_username_validation():
191+
# minimum length username is 4
192+
with pytest.raises(ValidationError) as err_info:
193+
MyProfilePatch.model_validate({"userName": "abc"})
194+
195+
assert err_info.value.error_count() == 1
196+
assert err_info.value.errors()[0]["type"] == "too_short"
197+
198+
MyProfilePatch.model_validate({"userName": "abcd"})
199+
200+
# Ensure valid characters (alphanumeric + . _ -)
201+
with pytest.raises(ValidationError, match="start with a letter") as err_info:
202+
MyProfilePatch.model_validate({"userName": "1234"})
203+
204+
assert err_info.value.error_count() == 1
205+
assert err_info.value.errors()[0]["type"] == "value_error"
206+
207+
MyProfilePatch.model_validate({"userName": "u1234"})

0 commit comments

Comments
 (0)