File tree 1 file changed +31
-0
lines changed
1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ from pydantic import BaseModel , validator
2
+ from typing import Optional
3
+
4
+ class UserSchema (BaseModel ):
5
+ username : str
6
+ password : str
7
+ api_key : Optional [str ] = None
8
+
9
+ @validator ("username" )
10
+ def username_must_be_unique (cls , value , values ):
11
+ # Access the database session from the FastAPI dependency
12
+ db = values .get ("db" )
13
+ if db :
14
+ existing_user = db .query (User ).filter (User .username == value ).first ()
15
+ if existing_user :
16
+ raise ValueError ("Username already exists." )
17
+ return value
18
+
19
+ @validator ("password" )
20
+ def password_must_be_at_least_8_characters (cls , value ):
21
+ if len (value ) < 8 :
22
+ raise ValueError ("Password must be at least 8 characters long." )
23
+ return value
24
+
25
+ @validator ("api_key" )
26
+ def api_key_must_be_valid (cls , value ):
27
+ if value is not None :
28
+ # Implement validation logic for API key format
29
+ # (e.g., length, character restrictions)
30
+ pass
31
+ return value
You can’t perform that action at this time.
0 commit comments