1+ from pydantic import EmailStr
12from sqlmodel import Field , Relationship , SQLModel
23
34
45# Shared properties
5- # TODO replace email str with EmailStr when sqlmodel supports it
66class UserBase (SQLModel ):
7- email : str = Field (unique = True , index = True )
7+ email : EmailStr = Field (unique = True , index = True , max_length = 255 )
88 is_active : bool = True
99 is_superuser : bool = False
10- full_name : str | None = None
10+ full_name : str | None = Field ( default = None , max_length = 255 )
1111
1212
1313# Properties to receive via API on creation
1414class UserCreate (UserBase ):
15- password : str
15+ password : str = Field ( min_length = 8 , max_length = 40 )
1616
1717
18- # TODO replace email str with EmailStr when sqlmodel supports it
1918class UserRegister (SQLModel ):
20- email : str
21- password : str
22- full_name : str | None = None
19+ email : EmailStr = Field ( max_length = 255 )
20+ password : str = Field ( min_length = 8 , max_length = 40 )
21+ full_name : str | None = Field ( default = None , max_length = 255 )
2322
2423
2524# Properties to receive via API on update, all are optional
26- # TODO replace email str with EmailStr when sqlmodel supports it
2725class UserUpdate (UserBase ):
28- email : str | None = None # type: ignore
29- password : str | None = None
26+ email : EmailStr | None = Field ( default = None , max_length = 255 ) # type: ignore
27+ password : str | None = Field ( default = None , min_length = 8 , max_length = 40 )
3028
3129
32- # TODO replace email str with EmailStr when sqlmodel supports it
3330class UserUpdateMe (SQLModel ):
34- full_name : str | None = None
35- email : str | None = None
31+ full_name : str | None = Field ( default = None , max_length = 255 )
32+ email : EmailStr | None = Field ( default = None , max_length = 255 )
3633
3734
3835class UpdatePassword (SQLModel ):
39- current_password : str
40- new_password : str
36+ current_password : str = Field ( min_length = 8 , max_length = 40 )
37+ new_password : str = Field ( min_length = 8 , max_length = 40 )
4138
4239
4340# Database model, database table inferred from class name
@@ -59,24 +56,24 @@ class UsersPublic(SQLModel):
5956
6057# Shared properties
6158class ItemBase (SQLModel ):
62- title : str
63- description : str | None = None
59+ title : str = Field ( min_length = 1 , max_length = 255 )
60+ description : str | None = Field ( default = None , max_length = 255 )
6461
6562
6663# Properties to receive on item creation
6764class ItemCreate (ItemBase ):
68- title : str
65+ title : str = Field ( min_length = 1 , max_length = 255 )
6966
7067
7168# Properties to receive on item update
7269class ItemUpdate (ItemBase ):
73- title : str | None = None # type: ignore
70+ title : str | None = Field ( default = None , min_length = 1 , max_length = 255 ) # type: ignore
7471
7572
7673# Database model, database table inferred from class name
7774class Item (ItemBase , table = True ):
7875 id : int | None = Field (default = None , primary_key = True )
79- title : str
76+ title : str = Field ( max_length = 255 )
8077 owner_id : int | None = Field (default = None , foreign_key = "user.id" , nullable = False )
8178 owner : User | None = Relationship (back_populates = "items" )
8279
@@ -110,4 +107,4 @@ class TokenPayload(SQLModel):
110107
111108class NewPassword (SQLModel ):
112109 token : str
113- new_password : str
110+ new_password : str = Field ( min_length = 8 , max_length = 40 )
0 commit comments