Skip to content
Merged

Dev #333

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions backend/app/core/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# 自定义日期字符串类型
DateStr = Annotated[
date,
AfterValidator(lambda x: date_validator(x)),
PlainSerializer(
lambda x: x.strftime("%Y-%m-%d") if isinstance(x, date) else str(x),
return_type=str,
Expand All @@ -31,6 +32,7 @@
# 自定义时间字符串类型
TimeStr = Annotated[
time,
AfterValidator(lambda x: time_validator(x)),
PlainSerializer(
lambda x: x.strftime("%H:%M:%S") if isinstance(x, time) else str(x),
return_type=str,
Expand Down Expand Up @@ -78,6 +80,50 @@ def datetime_validator(value: str | datetime) -> datetime:
raise CustomException(code=RET.ERROR.code, msg="无效的日期格式")


def date_validator(value: str | date) -> date:
"""
日期格式验证器。

参数:
- value (str | date): 日期值。

返回:
- date: 格式化后的日期。

异常:
- CustomException: 日期格式无效时抛出。
"""
try:
if isinstance(value, str):
return datetime.strptime(value, "%Y-%m-%d").date()
if isinstance(value, date):
return value
except Exception:
raise CustomException(code=RET.ERROR.code, msg="无效的日期格式")


def time_validator(value: str | time) -> time:
"""
时间格式验证器。

参数:
- value (str | time): 时间值。

返回:
- time: 格式化后的时间。

异常:
- CustomException: 时间格式无效时抛出。
"""
try:
if isinstance(value, str):
return datetime.strptime(value, "%H:%M:%S").time()
if isinstance(value, time):
return value
except Exception:
raise CustomException(code=RET.ERROR.code, msg="无效的时间格式")


def email_validator(value: str) -> str:
"""
邮箱地址验证器。
Expand Down
Binary file modified backend/data/group.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading