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: 手机区号标识检查 #1347

Merged
merged 4 commits into from
Nov 13, 2023
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
3 changes: 3 additions & 0 deletions src/bk-user/bkuser/apis/web/personal_center/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ def validate(self, attrs):
if not attrs.get("custom_phone"):
raise ValidationError(_("自定义手机号码为必填项"))

if "+" in attrs["custom_phone_country_code"]:
raise ValidationError(_("区号设置无需携带标识:'+'"))

try:
validate_phone_with_country_code(
phone=attrs["custom_phone"], country_code=attrs["custom_phone_country_code"]
Expand Down
6 changes: 5 additions & 1 deletion src/bk-user/bkuser/common/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import logging
narasux marked this conversation as resolved.
Show resolved Hide resolved

import phonenumbers
from phonenumbers import NumberParseException, region_code_for_country_code
from phonenumbers import UNKNOWN_REGION, NumberParseException, region_code_for_country_code

logger = logging.getLogger(__name__)

Expand All @@ -26,6 +26,10 @@ def validate_phone_with_country_code(phone: str, country_code: str) -> None:
except Exception:
raise ValueError(f"parse phone country code [{country_code}] to region failed!")

# 解析出未知区号
if region == UNKNOWN_REGION:
raise ValueError(f"unknown phone country code: {country_code}")

# 特殊检查:中国手机号强制要求必须是 11 位
if region == "CN" and len(phone) != 11: # noqa: PLR2004
raise ValueError(f"chinese phone number must be 11 digits, {phone} is invalid")
Expand Down
Loading