🗃️ Add max_length validation for database models and input data (#1233)
This commit is contained in:
@@ -1,43 +1,40 @@
|
||||
from pydantic import EmailStr
|
||||
from sqlmodel import Field, Relationship, SQLModel
|
||||
|
||||
|
||||
# Shared properties
|
||||
# TODO replace email str with EmailStr when sqlmodel supports it
|
||||
class UserBase(SQLModel):
|
||||
email: str = Field(unique=True, index=True)
|
||||
email: EmailStr = Field(unique=True, index=True, max_length=255)
|
||||
is_active: bool = True
|
||||
is_superuser: bool = False
|
||||
full_name: str | None = None
|
||||
full_name: str | None = Field(default=None, max_length=255)
|
||||
|
||||
|
||||
# Properties to receive via API on creation
|
||||
class UserCreate(UserBase):
|
||||
password: str
|
||||
password: str = Field(min_length=8, max_length=40)
|
||||
|
||||
|
||||
# TODO replace email str with EmailStr when sqlmodel supports it
|
||||
class UserRegister(SQLModel):
|
||||
email: str
|
||||
password: str
|
||||
full_name: str | None = None
|
||||
email: EmailStr = Field(max_length=255)
|
||||
password: str = Field(min_length=8, max_length=40)
|
||||
full_name: str | None = Field(default=None, max_length=255)
|
||||
|
||||
|
||||
# Properties to receive via API on update, all are optional
|
||||
# TODO replace email str with EmailStr when sqlmodel supports it
|
||||
class UserUpdate(UserBase):
|
||||
email: str | None = None # type: ignore
|
||||
password: str | None = None
|
||||
email: EmailStr | None = Field(default=None, max_length=255) # type: ignore
|
||||
password: str | None = Field(default=None, min_length=8, max_length=40)
|
||||
|
||||
|
||||
# TODO replace email str with EmailStr when sqlmodel supports it
|
||||
class UserUpdateMe(SQLModel):
|
||||
full_name: str | None = None
|
||||
email: str | None = None
|
||||
full_name: str | None = Field(default=None, max_length=255)
|
||||
email: EmailStr | None = Field(default=None, max_length=255)
|
||||
|
||||
|
||||
class UpdatePassword(SQLModel):
|
||||
current_password: str
|
||||
new_password: str
|
||||
current_password: str = Field(min_length=8, max_length=40)
|
||||
new_password: str = Field(min_length=8, max_length=40)
|
||||
|
||||
|
||||
# Database model, database table inferred from class name
|
||||
@@ -59,24 +56,24 @@ class UsersPublic(SQLModel):
|
||||
|
||||
# Shared properties
|
||||
class ItemBase(SQLModel):
|
||||
title: str
|
||||
description: str | None = None
|
||||
title: str = Field(min_length=1, max_length=255)
|
||||
description: str | None = Field(default=None, max_length=255)
|
||||
|
||||
|
||||
# Properties to receive on item creation
|
||||
class ItemCreate(ItemBase):
|
||||
title: str
|
||||
title: str = Field(min_length=1, max_length=255)
|
||||
|
||||
|
||||
# Properties to receive on item update
|
||||
class ItemUpdate(ItemBase):
|
||||
title: str | None = None # type: ignore
|
||||
title: str | None = Field(default=None, min_length=1, max_length=255) # type: ignore
|
||||
|
||||
|
||||
# Database model, database table inferred from class name
|
||||
class Item(ItemBase, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
title: str
|
||||
title: str = Field(max_length=255)
|
||||
owner_id: int | None = Field(default=None, foreign_key="user.id", nullable=False)
|
||||
owner: User | None = Relationship(back_populates="items")
|
||||
|
||||
@@ -110,4 +107,4 @@ class TokenPayload(SQLModel):
|
||||
|
||||
class NewPassword(SQLModel):
|
||||
token: str
|
||||
new_password: str
|
||||
new_password: str = Field(min_length=8, max_length=40)
|
||||
|
Reference in New Issue
Block a user