当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 3

AIDY/protoc-go-valid
暂停

forked from A-涛/protoc-go-valid 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
validurl.go 3.09 KB
一键复制 编辑 原始数据 按行查看 历史
A-涛 提交于 2022-11-17 12:11 . 调整规范
package valid
import (
"errors"
"net/url"
"reflect"
"strings"
)
// VUrl 验证 url
type VUrl struct {
ruleObj RM
errBuf *strings.Builder
vc *validCommon
}
// NewVUrl 验证 url
func NewVUrl() *VUrl {
obj := new(VUrl)
obj.errBuf = newStrBuf()
obj.vc = new(validCommon)
return obj
}
// SetRule 添加验证规则
func (v *VUrl) SetRule(ruleObj RM) *VUrl {
v.ruleObj = ruleObj
return v
}
// Valid 验证
func (v *VUrl) Valid(src interface{}) error {
if src == nil {
return errors.New("src is nil")
}
var srcStr string
switch v := src.(type) {
case string:
srcStr = v
case *string:
srcStr = *v
default:
return errors.New("src must is string/*string")
}
return v.validate(srcStr).getError()
}
// SetValidFn 自定义设置验证函数
func (v *VUrl) SetValidFn(validName string, fn CommonValidFn) *VUrl {
v.vc.setValidFn(validName, fn)
return v
}
// getValidFn 获取验证函数
func (v *VUrl) getValidFn(validName string) (CommonValidFn, error) {
return v.vc.getValidFn(validName)
}
// validate 验证执行体
func (v *VUrl) validate(value string) *VUrl {
// 解码处理
decUrl, err := url.QueryUnescape(value)
if err != nil {
v.errBuf.WriteString(GetJoinFieldErr("", "", "url unescape is failed, err: "+err.Error()))
return v
}
urlQuery := ""
queryIndex := strings.Index(decUrl, "?")
if queryIndex != -1 {
urlQuery = decUrl[queryIndex+1:]
}
if urlQuery == "" {
return v
}
var key, val string
for _, query := range strings.Split(urlQuery, "&") {
key = ""
val = ""
key2val := strings.Split(query, "=")
l := len(key2val)
if l > 0 {
key = key2val[0]
}
if l > 1 {
val = key2val[1]
}
validNames := v.ruleObj.Get(key)
if validNames == "" {
continue
}
// 根据验证内容进行验证
for _, validName := range ValidNamesSplit(validNames) {
if validName == "" {
continue
}
validKey, _, cusMsg := ParseValidNameKV(validName)
fn, err := v.getValidFn(validKey)
if err != nil {
v.errBuf.WriteString(GetJoinFieldErr("", key, err))
continue
}
// 开始验证
if fn == nil {
switch validKey {
case Required:
if val != "" { // 验证必填
continue
}
if cusMsg != "" {
v.errBuf.WriteString(GetJoinValidErrStr("", key, "", cusMsg))
continue
}
v.errBuf.WriteString(GetJoinValidErrStr("", key, "", ExplainEn, "it is", Required))
case Either, BothEq:
v.vc.initValid2FieldsMap(&name2Value{
validName: validName,
fieldName: key,
cusMsg: cusMsg,
reflectVal: reflect.ValueOf(val),
})
default:
v.errBuf.WriteString(GetJoinFieldErr("", key, "valid \""+validName+"\" is no support"))
}
continue
}
// 拓展的验证方法
if val == "" { // 空就直接跳过
continue
}
fn(v.errBuf, validName, "", key, reflect.ValueOf(val))
}
}
return v
}
// getError 获取 err
func (v *VUrl) getError() error {
defer putStrBuf(v.errBuf)
v.vc.valid(v.errBuf)
if v.errBuf.Len() == 0 {
return nil
}
// 这里需要去掉最后一个 ErrEndFlag
return errors.New(strings.TrimSuffix(v.errBuf.String(), ErrEndFlag))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/aidyfocus/protoc-go-valid.git
git@gitee.com:aidyfocus/protoc-go-valid.git
aidyfocus
protoc-go-valid
protoc-go-valid
v1.6.19

搜索帮助