feat(internal): 账号配置验证器
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package bilibili
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// BilibiliAccountConfigValidator B站账号配置验证器
|
||||
type BilibiliAccountConfigValidator struct{}
|
||||
|
||||
func (v *BilibiliAccountConfigValidator) GetType() string {
|
||||
return "bilibili-account"
|
||||
}
|
||||
|
||||
func (v *BilibiliAccountConfigValidator) Validate(config string) error {
|
||||
var cfg BiliAccount
|
||||
if err := json.Unmarshal([]byte(config), &cfg); err != nil {
|
||||
return fmt.Errorf("解析B站账号配置失败: %v", err)
|
||||
}
|
||||
|
||||
if cfg.RawCookies == "" {
|
||||
return fmt.Errorf("B站账号配置中 cookies 不能为空")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// EmailAccountConfigValidator 邮箱账号配置验证器
|
||||
type EmailAccountConfigValidator struct{}
|
||||
|
||||
func (v *EmailAccountConfigValidator) GetType() string {
|
||||
return "email-account"
|
||||
}
|
||||
|
||||
func (v *EmailAccountConfigValidator) Validate(config string) error {
|
||||
// 邮箱账号配置验证逻辑
|
||||
var cfg EmailAccountConfig
|
||||
if err := json.Unmarshal([]byte(config), &cfg); err != nil {
|
||||
return fmt.Errorf("解析邮箱账号配置失败: %v", err)
|
||||
}
|
||||
|
||||
if cfg.SMTPServer == "" {
|
||||
return fmt.Errorf("邮箱账号配置中 SMTP 服务器不能为空")
|
||||
}
|
||||
if cfg.Port == 0 {
|
||||
return fmt.Errorf("邮箱账号配置中 SMTP 端口不能为空")
|
||||
}
|
||||
if cfg.Username == "" {
|
||||
return fmt.Errorf("邮箱账号配置中用户名不能为空")
|
||||
}
|
||||
if cfg.Password == "" {
|
||||
return fmt.Errorf("邮箱账号配置中密码不能为空")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package accountConfigChecker
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// AccountValidator 账号验证器接口
|
||||
type AccountValidator interface {
|
||||
// Validate 验证账号配置是否合法
|
||||
Validate(account string) error
|
||||
// GetType 获取验证器支持的账号类型
|
||||
GetType() string
|
||||
}
|
||||
|
||||
// ValidatorRegistry 验证器注册表
|
||||
type ValidatorRegistry struct {
|
||||
validators map[string]AccountValidator
|
||||
}
|
||||
|
||||
// NewValidatorRegistry 创建新的验证器注册表
|
||||
func NewValidatorRegistry() *ValidatorRegistry {
|
||||
return &ValidatorRegistry{
|
||||
validators: make(map[string]AccountValidator),
|
||||
}
|
||||
}
|
||||
|
||||
// Register 注册账号验证器
|
||||
func (vr *ValidatorRegistry) Register(validator AccountValidator) {
|
||||
vr.validators[validator.GetType()] = validator
|
||||
}
|
||||
|
||||
// Validate 根据账号类型验证配置
|
||||
func (vr *ValidatorRegistry) Validate(accountType string, config string) error {
|
||||
validator, exists := vr.validators[accountType]
|
||||
if !exists {
|
||||
return fmt.Errorf("不支持的账号类型: %s", accountType)
|
||||
}
|
||||
|
||||
if config == "" {
|
||||
return fmt.Errorf("账号配置不能为空")
|
||||
}
|
||||
|
||||
// 首先验证 JSON 格式是否正确
|
||||
var js json.RawMessage
|
||||
if err := json.Unmarshal([]byte(config), &js); err != nil {
|
||||
return fmt.Errorf("账号配置 JSON 格式错误: %v", err)
|
||||
}
|
||||
|
||||
// 调用具体验证器的验证逻辑
|
||||
return validator.Validate(config)
|
||||
}
|
||||
|
||||
// GetSupportedTypes 获取所有支持的账号类型
|
||||
func (vr *ValidatorRegistry) GetSupportedTypes() []string {
|
||||
types := make([]string, 0, len(vr.validators))
|
||||
for t := range vr.validators {
|
||||
types = append(types, t)
|
||||
}
|
||||
return types
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package accountConfigChecker
|
||||
|
||||
func InitValidatorRegistry() *ValidatorRegistry {
|
||||
registry := NewValidatorRegistry()
|
||||
|
||||
// 注册所有规则验证器
|
||||
// registry.Register(&bilibili.BilibiliAccountValidator{})
|
||||
|
||||
return registry
|
||||
}
|
||||
Reference in New Issue
Block a user