You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.9 KiB

"""
验证器模块
"""
import re
from datetime import date
from typing import Tuple, Optional
def validate_code_format(code: str) -> Tuple[bool, str]:
"""
验证代码格式
Returns:
(是否有效, 错误信息)
"""
if not code:
return False, "代码不能为空"
# 股票代码格式: 000001.SZ, 600000.SH
stock_pattern = r"^\d{6}\.(SZ|SH|BJ)$"
# 期货代码格式: IF2501.CFE
future_pattern = r"^[A-Z]{1,2}\d{4}\.CFE$"
if re.match(stock_pattern, code):
return True, ""
if re.match(future_pattern, code):
return True, ""
return False, f"无效的代码格式: {code}"
def validate_date_range(start_date: date, end_date: date) -> Tuple[bool, str]:
"""
验证日期范围
Returns:
(是否有效, 错误信息)
"""
if start_date > end_date:
return False, "开始日期不能晚于结束日期"
# 检查日期范围是否合理不超过5年
from datetime import timedelta
if (end_date - start_date).days > 365 * 5:
return False, "日期范围不能超过5年"
return True, ""
def validate_period_type(period: str) -> Tuple[bool, str]:
"""
验证周期类型
Returns:
(是否有效, 错误信息)
"""
valid_periods = ["daily", "min1", "min5", "min15", "min30", "min60"]
if period not in valid_periods:
return False, f"无效的周期类型: {period},有效值: {', '.join(valid_periods)}"
return True, ""
def validate_security_type(security_type: str) -> Tuple[bool, str]:
"""
验证证券类型
Returns:
(是否有效, 错误信息)
"""
valid_types = ["stock", "future", "index", "etf", "kzz"]
if security_type not in valid_types:
return False, f"无效的证券类型: {security_type},有效值: {', '.join(valid_types)}"
return True, ""