File tree Expand file tree Collapse file tree 2 files changed +22
-2
lines changed
packages/models-library/src/models_library/api_schemas_webserver
services/web/server/tests/unit/isolated Expand file tree Collapse file tree 2 files changed +22
-2
lines changed Original file line number Diff line number Diff line change @@ -141,7 +141,7 @@ def from_domain_model(
141141class 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
Original file line number Diff line number Diff line change 1818from models_library .generics import Envelope
1919from models_library .users import UserThirdPartyToken
2020from models_library .utils .fastapi_encoders import jsonable_encoder
21- from pydantic import BaseModel
21+ from pydantic import BaseModel , ValidationError
2222from 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" })
You can’t perform that action at this time.
0 commit comments