<commit_before>package common

import (
	"fmt"
	"strings"
)

// RuleConfig 验证规则配置
type RuleConfig struct {
	Name       string
	Desc       string              //规则描述
	Validators []map[string]string //验证器列表,必须要有name
}

// ValidateRuleConfig 验证规则配置
type ValidateRuleConfig struct {
	Rules  []RuleConfig
	SName  string          //服务的名称
	parsed validateRuleMap //解析后的结果
}

// Parse 解析验证的配置
func (p *ValidateRuleConfig) Parse() error {
	if p == nil {
		Warnf("no validate conf")
		return nil
	}
	rules := make(validateRuleMap)
	for _, ruleConfig := range p.Rules {
		ruleName := strings.TrimSpace(ruleConfig.Name)
		if len(ruleName) == 0 {
			panic("The rule name must not be empty")
		}
		validators := make([]StrValidator, 0, len(ruleConfig.Validators))
		for _, validatorConf := range ruleConfig.Validators {
			validators = append(validators, NewValidatorByConf(validatorConf))
		}
		rule := &ValidateRule{
			desc:       ruleConfig.Desc,
			validators: validators}
		rules[ruleName] = rule
		Debugf("Add validate rule:%s", ruleName)
	}
	p.parsed = rules
	return nil
}

// NewService 根据配置解析的结果创建验证服务
func (p *ValidateRuleConfig) NewService() Service {
	if p.parsed == nil {
		panic("Can't create ValidateService from nil")
	}
	svr := RuleValidateService{}
	svr.SName = p.SName
	svr.rules = p.parsed
	return &svr
}

//ValidateRule 定义验证规则
type ValidateRule struct {
	desc       string         //规则描述
	validators []StrValidator //通过Rules构建出来的验证规则
}

type validateRuleMap map[string]*ValidateRule

// ValidateConfigurer validateConfig
type ValidateConfigurer interface {
	GetValidateRuleConfig() *ValidateRuleConfig
}

// ValidateService 验证服务
type ValidateService interface {
	Service
	//Validate 使用name指定验证规则,对value进行验证,验证通过返回nil,否则返回错误原因
	Validate(name string, value string) error
}

// RuleValidateService  根据规则进行的验证服务
type RuleValidateService struct {
	BaseService
	Config ValidateConfigurer `inject:"_"`
	rules  validateRuleMap
}

// Init implements Initable
func (p *RuleValidateService) Init() error {
	if p.Config == nil {
		Warnf("no validate config")
		return nil
	}
	if p.Config.GetValidateRuleConfig() == nil {
		return fmt.Errorf("no validate config rule")
	}
	config := p.Config.GetValidateRuleConfig()<commit_msg>allow validate rule absent<commit_after>package common

import (
	"fmt"
	"strings"
)

// RuleConfig 验证规则配置
type RuleConfig struct {
	Name       string
	Desc       string              //规则描述
	Validators []map[string]string //验证器列表,必须要有name
}

// ValidateRuleConfig 验证规则配置
type ValidateRuleConfig struct {
	Rules  []RuleConfig
	SName  string          //服务的名称
	parsed validateRuleMap //解析后的结果
}

// Parse 解析验证的配置
func (p *ValidateRuleConfig) Parse() error {
	if p == nil {
		Warnf("no validate conf")
		return nil
	}
	rules := make(validateRuleMap)
	for _, ruleConfig := range p.Rules {
		ruleName := strings.TrimSpace(ruleConfig.Name)
		if len(ruleName) == 0 {
			panic("The rule name must not be empty")
		}
		validators := make([]StrValidator, 0, len(ruleConfig.Validators))
		for _, validatorConf := range ruleConfig.Validators {
			validators = append(validators, NewValidatorByConf(validatorConf))
		}
		rule := &ValidateRule{
			desc:       ruleConfig.Desc,
			validators: validators}
		rules[ruleName] = rule
		Debugf("Add validate rule:%s", ruleName)
	}
	p.parsed = rules
	return nil
}

// NewService 根据配置解析的结果创建验证服务
func (p *ValidateRuleConfig) NewService() Service {
	if p.parsed == nil {
		panic("Can't create ValidateService from nil")
	}
	svr := RuleValidateService{}
	svr.SName = p.SName
	svr.rules = p.parsed
	return &svr
}

//ValidateRule 定义验证规则
type ValidateRule struct {
	desc       string         //规则描述
	validators []StrValidator //通过Rules构建出来的验证规则
}

type validateRuleMap map[string]*ValidateRule

// ValidateConfigurer validateConfig
type ValidateConfigurer interface {
	GetValidateRuleConfig() *ValidateRuleConfig
}

// ValidateService 验证服务
type ValidateService interface {
	Service
	//Validate 使用name指定验证规则,对value进行验证,验证通过返回nil,否则返回错误原因
	Validate(name string, value string) error
}

// RuleValidateService  根据规则进行的验证服务
type RuleValidateService struct {
	BaseService
	Config ValidateConfigurer `inject:"_"`
	rules  validateRuleMap
}

// Init implements Initable
func (p *RuleValidateService) Init() error {
	if p.Config == nil {
		Warnf("no validate config")
		return nil
	}
	if p.Config.GetValidateRuleConfig() == nil {
		Warnf("no validate config rule")
		return nil
	}
	config := p.Config.GetValidateRuleConfig()
{'fraction_non_alphanumeric': 0.10116644235082997, 'fraction_numerical': 0.0008972633467922835, 'mean_word_length': 4.021396396396397, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5187402', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1716, 'n_words': 373}