Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复主页为空字符串也能通过测试的问题 #226

Merged
merged 1 commit into from
Jul 18, 2024
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/

## [Unreleased]

### Fixed

- 修复主页为空字符串也能通过测试的问题

## [3.3.2] - 2024-05-01

### Added
Expand Down
1 change: 0 additions & 1 deletion src/utils/validation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def validate_info(
if publish_type not in validation_model_map:
raise ValueError("⚠️ 未知的发布类型。") # pragma: no cover

# 如果升级至 pydantic 2 后,可以使用 validation-context
# https://docs.pydantic.dev/latest/usage/validators/#validation-context
validation_context = {
"previous_data": raw_data.get("previous_data"),
Expand Down
1 change: 1 addition & 0 deletions src/utils/validation/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
"color_error": "颜色格式不正确",
"string_too_long": "字符串长度不能超过 {max_length} 个字符",
"too_long": "列表长度不能超过 {max_length} 个元素",
"string_pattern_mismatch": "字符串应满足格式 '{pattern}'",
}
13 changes: 11 additions & 2 deletions src/utils/validation/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import abc
import json
from enum import Enum
from typing import TYPE_CHECKING, Any, TypedDict
from typing import TYPE_CHECKING, Annotated, Any, TypedDict

from pydantic import (
BaseModel,
Field,
StringConstraints,
ValidationInfo,
ValidatorFunctionWrapHandler,
field_validator,
Expand Down Expand Up @@ -111,7 +112,10 @@ class PublishInfo(abc.ABC, BaseModel):
name: str = Field(max_length=NAME_MAX_LENGTH)
desc: str
author: str
homepage: str
homepage: Annotated[
str,
StringConstraints(strip_whitespace=True, pattern=r"^https?://.*$"),
]
tags: list[Tag] = Field(max_length=3)
is_official: bool = False

Expand All @@ -120,6 +124,11 @@ class PublishInfo(abc.ABC, BaseModel):
def collect_valid_values(
cls, v: Any, handler: ValidatorFunctionWrapHandler, info: ValidationInfo
):
"""收集验证通过的数据

NOTE: 其他所有的验证器都应该在这个验证器之前执行
所以不能用 after 模式,只能用 before 模式
"""
context = info.context
if context is None: # pragma: no cover
raise PydanticCustomError("validation_context", "未获取到验证上下文")
Expand Down
25 changes: 25 additions & 0 deletions tests/utils/validation/fields/test_homepage.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,28 @@ async def test_homepage_failed_http_exception(mocked_api: MockRouter) -> None:
]

assert mocked_api["exception"].called


async def test_homepage_failed_empty_homepage(mocked_api: MockRouter) -> None:
"""主页为空字符串的情况"""
from src.utils.validation import PublishType, validate_info

data = generate_bot_data(homepage="")

result = validate_info(PublishType.BOT, data)

assert not result["valid"]
assert "homepage" not in result["data"]
assert result["errors"] == [
{
"type": "string_pattern_mismatch",
"loc": ("homepage",),
"msg": "字符串应满足格式 '^https?://.*$'",
"input": "",
"ctx": {"pattern": "^https?://.*$"},
"url": "https://errors.pydantic.dev/2.7/v/string_pattern_mismatch",
}
]

assert not mocked_api["homepage"].called
assert not mocked_api["homepage_failed"].called
Loading