mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
improve package gvalid
This commit is contained in:
@ -18,7 +18,7 @@ import (
|
||||
type utilPProf struct{}
|
||||
|
||||
const (
|
||||
gDEFAULT_PPROF_PATTERN = "/debug/pprof"
|
||||
defaultPProfPattern = "/debug/pprof"
|
||||
)
|
||||
|
||||
// EnablePProf enables PProf feature for server.
|
||||
@ -28,7 +28,7 @@ func (s *Server) EnablePProf(pattern ...string) {
|
||||
|
||||
// EnablePProf enables PProf feature for server of specified domain.
|
||||
func (d *Domain) EnablePProf(pattern ...string) {
|
||||
p := gDEFAULT_PPROF_PATTERN
|
||||
p := defaultPProfPattern
|
||||
if len(pattern) > 0 && pattern[0] != "" {
|
||||
p = pattern[0]
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -8,6 +8,7 @@
|
||||
package gvalid
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/text/gregex"
|
||||
@ -62,6 +63,136 @@ import (
|
||||
// like: map[field] => string|map[rule]string
|
||||
type CustomMsg = map[string]interface{}
|
||||
|
||||
const (
|
||||
// regular expression pattern for single validation rule.
|
||||
singleRulePattern = `^([\w-]+):{0,1}(.*)`
|
||||
invalidRulesErrKey = "invalid_rules"
|
||||
invalidParamsErrKey = "invalid_params"
|
||||
invalidObjectErrKey = "invalid_object"
|
||||
)
|
||||
|
||||
var (
|
||||
// defaultValidator is the default validator for package functions.
|
||||
defaultValidator = New()
|
||||
|
||||
// all internal error keys.
|
||||
internalErrKeyMap = map[string]string{
|
||||
invalidRulesErrKey: invalidRulesErrKey,
|
||||
invalidParamsErrKey: invalidParamsErrKey,
|
||||
invalidObjectErrKey: invalidObjectErrKey,
|
||||
}
|
||||
// regular expression object for single rule
|
||||
// which is compiled just once and of repeatable usage.
|
||||
ruleRegex, _ = regexp.Compile(singleRulePattern)
|
||||
|
||||
// mustCheckRulesEvenValueEmpty specifies some rules that must be validated
|
||||
// even the value is empty (nil or empty).
|
||||
mustCheckRulesEvenValueEmpty = map[string]struct{}{
|
||||
"required": {},
|
||||
"required-if": {},
|
||||
"required-unless": {},
|
||||
"required-with": {},
|
||||
"required-with-all": {},
|
||||
"required-without": {},
|
||||
"required-without-all": {},
|
||||
//"same": {},
|
||||
//"different": {},
|
||||
//"in": {},
|
||||
//"not-in": {},
|
||||
//"regex": {},
|
||||
}
|
||||
// allSupportedRules defines all supported rules that is used for quick checks.
|
||||
allSupportedRules = map[string]struct{}{
|
||||
"required": {},
|
||||
"required-if": {},
|
||||
"required-unless": {},
|
||||
"required-with": {},
|
||||
"required-with-all": {},
|
||||
"required-without": {},
|
||||
"required-without-all": {},
|
||||
"date": {},
|
||||
"date-format": {},
|
||||
"email": {},
|
||||
"phone": {},
|
||||
"phone-loose": {},
|
||||
"telephone": {},
|
||||
"passport": {},
|
||||
"password": {},
|
||||
"password2": {},
|
||||
"password3": {},
|
||||
"postcode": {},
|
||||
"resident-id": {},
|
||||
"bank-card": {},
|
||||
"qq": {},
|
||||
"ip": {},
|
||||
"ipv4": {},
|
||||
"ipv6": {},
|
||||
"mac": {},
|
||||
"url": {},
|
||||
"domain": {},
|
||||
"length": {},
|
||||
"min-length": {},
|
||||
"max-length": {},
|
||||
"between": {},
|
||||
"min": {},
|
||||
"max": {},
|
||||
"json": {},
|
||||
"integer": {},
|
||||
"float": {},
|
||||
"boolean": {},
|
||||
"same": {},
|
||||
"different": {},
|
||||
"in": {},
|
||||
"not-in": {},
|
||||
"regex": {},
|
||||
}
|
||||
// boolMap defines the boolean values.
|
||||
boolMap = map[string]struct{}{
|
||||
"1": {},
|
||||
"true": {},
|
||||
"on": {},
|
||||
"yes": {},
|
||||
"": {},
|
||||
"0": {},
|
||||
"false": {},
|
||||
"off": {},
|
||||
"no": {},
|
||||
}
|
||||
)
|
||||
|
||||
// Check checks single value with specified rules.
|
||||
// It returns nil if successful validation.
|
||||
//
|
||||
// The parameter <value> can be any type of variable, which will be converted to string
|
||||
// for validation.
|
||||
// The parameter <rules> can be one or more rules, multiple rules joined using char '|'.
|
||||
// The parameter <messages> specifies the custom error messages, which can be type of:
|
||||
// string/map/struct/*struct.
|
||||
// The optional parameter <params> specifies the extra validation parameters for some rules
|
||||
// like: required-*、same、different, etc.
|
||||
func Check(value interface{}, rules string, messages interface{}, params ...interface{}) *Error {
|
||||
return defaultValidator.Check(value, rules, messages, params...)
|
||||
}
|
||||
|
||||
// CheckMap validates map and returns the error result. It returns nil if with successful validation.
|
||||
//
|
||||
// The parameter <rules> can be type of []string/map[string]string. It supports sequence in error result
|
||||
// if <rules> is type of []string.
|
||||
// The optional parameter <messages> specifies the custom error messages for specified keys and rules.
|
||||
func CheckMap(params interface{}, rules interface{}, messages ...CustomMsg) *Error {
|
||||
return defaultValidator.CheckMap(params, rules, messages...)
|
||||
}
|
||||
|
||||
// CheckStruct validates strcut and returns the error result.
|
||||
//
|
||||
// The parameter <object> should be type of struct/*struct.
|
||||
// The parameter <rules> can be type of []string/map[string]string. It supports sequence in error result
|
||||
// if <rules> is type of []string.
|
||||
// The optional parameter <messages> specifies the custom error messages for specified keys and rules.
|
||||
func CheckStruct(object interface{}, rules interface{}, messages ...CustomMsg) *Error {
|
||||
return defaultValidator.CheckStruct(object, rules, messages...)
|
||||
}
|
||||
|
||||
// parseSequenceTag parses one sequence tag to field, rule and error message.
|
||||
// The sequence tag is like: [alias@]rule[...#msg...]
|
||||
func parseSequenceTag(tag string) (field, rule, msg string) {
|
||||
|
||||
31
util/gvalid/gvalid_validator.go
Normal file
31
util/gvalid/gvalid_validator.go
Normal file
@ -0,0 +1,31 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gvalid
|
||||
|
||||
// Validator is the validation manager.
|
||||
type Validator struct {
|
||||
i18nLang string // I18n language.
|
||||
}
|
||||
|
||||
// New creates and returns a new Validator.
|
||||
func New() *Validator {
|
||||
return &Validator{}
|
||||
}
|
||||
|
||||
// Clone creates and returns a new Validator which is a shallow copy of current one.
|
||||
func (v *Validator) Clone() *Validator {
|
||||
newValidator := New()
|
||||
*newValidator = *v
|
||||
return newValidator
|
||||
}
|
||||
|
||||
// I18n is a chaining operation function which sets the I18n language for next validation.
|
||||
func (v *Validator) I18n(language string) *Validator {
|
||||
newValidator := v.Clone()
|
||||
newValidator.i18nLang = language
|
||||
return newValidator
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -14,105 +14,10 @@ import (
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
"github.com/gogf/gf/text/gregex"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// regular expression pattern for single validation rule.
|
||||
singleRulePattern = `^([\w-]+):{0,1}(.*)`
|
||||
invalidRulesErrKey = "invalid_rules"
|
||||
invalidParamsErrKey = "invalid_params"
|
||||
invalidObjectErrKey = "invalid_object"
|
||||
)
|
||||
|
||||
var (
|
||||
// all internal error keys.
|
||||
internalErrKeyMap = map[string]string{
|
||||
invalidRulesErrKey: invalidRulesErrKey,
|
||||
invalidParamsErrKey: invalidParamsErrKey,
|
||||
invalidObjectErrKey: invalidObjectErrKey,
|
||||
}
|
||||
// regular expression object for single rule
|
||||
// which is compiled just once and of repeatable usage.
|
||||
ruleRegex, _ = regexp.Compile(singleRulePattern)
|
||||
|
||||
// mustCheckRulesEvenValueEmpty specifies some rules that must be validated
|
||||
// even the value is empty (nil or empty).
|
||||
mustCheckRulesEvenValueEmpty = map[string]struct{}{
|
||||
"required": {},
|
||||
"required-if": {},
|
||||
"required-unless": {},
|
||||
"required-with": {},
|
||||
"required-with-all": {},
|
||||
"required-without": {},
|
||||
"required-without-all": {},
|
||||
//"same": {},
|
||||
//"different": {},
|
||||
//"in": {},
|
||||
//"not-in": {},
|
||||
//"regex": {},
|
||||
}
|
||||
// allSupportedRules defines all supported rules that is used for quick checks.
|
||||
allSupportedRules = map[string]struct{}{
|
||||
"required": {},
|
||||
"required-if": {},
|
||||
"required-unless": {},
|
||||
"required-with": {},
|
||||
"required-with-all": {},
|
||||
"required-without": {},
|
||||
"required-without-all": {},
|
||||
"date": {},
|
||||
"date-format": {},
|
||||
"email": {},
|
||||
"phone": {},
|
||||
"phone-loose": {},
|
||||
"telephone": {},
|
||||
"passport": {},
|
||||
"password": {},
|
||||
"password2": {},
|
||||
"password3": {},
|
||||
"postcode": {},
|
||||
"resident-id": {},
|
||||
"bank-card": {},
|
||||
"qq": {},
|
||||
"ip": {},
|
||||
"ipv4": {},
|
||||
"ipv6": {},
|
||||
"mac": {},
|
||||
"url": {},
|
||||
"domain": {},
|
||||
"length": {},
|
||||
"min-length": {},
|
||||
"max-length": {},
|
||||
"between": {},
|
||||
"min": {},
|
||||
"max": {},
|
||||
"json": {},
|
||||
"integer": {},
|
||||
"float": {},
|
||||
"boolean": {},
|
||||
"same": {},
|
||||
"different": {},
|
||||
"in": {},
|
||||
"not-in": {},
|
||||
"regex": {},
|
||||
}
|
||||
// boolMap defines the boolean values.
|
||||
boolMap = map[string]struct{}{
|
||||
"1": {},
|
||||
"true": {},
|
||||
"on": {},
|
||||
"yes": {},
|
||||
"": {},
|
||||
"0": {},
|
||||
"false": {},
|
||||
"off": {},
|
||||
"no": {},
|
||||
}
|
||||
)
|
||||
|
||||
// Check checks single value with specified rules.
|
||||
// It returns nil if successful validation.
|
||||
//
|
||||
@ -123,12 +28,12 @@ var (
|
||||
// string/map/struct/*struct.
|
||||
// The optional parameter <params> specifies the extra validation parameters for some rules
|
||||
// like: required-*、same、different, etc.
|
||||
func Check(value interface{}, rules string, messages interface{}, params ...interface{}) *Error {
|
||||
return doCheck("", value, rules, messages, params...)
|
||||
func (v *Validator) Check(value interface{}, rules string, messages interface{}, params ...interface{}) *Error {
|
||||
return v.doCheck("", value, rules, messages, params...)
|
||||
}
|
||||
|
||||
// doCheck does the really rules validation for single key-value.
|
||||
func doCheck(key string, value interface{}, rules string, messages interface{}, params ...interface{}) *Error {
|
||||
func (v *Validator) doCheck(key string, value interface{}, rules string, messages interface{}, params ...interface{}) *Error {
|
||||
// If there's no validation rules, it does nothing and returns quickly.
|
||||
if rules == "" {
|
||||
return nil
|
||||
@ -196,7 +101,7 @@ func doCheck(key string, value interface{}, rules string, messages interface{},
|
||||
// It checks custom validation rules with most priority.
|
||||
var (
|
||||
dataMap map[string]interface{}
|
||||
message = getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
message = v.getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
)
|
||||
if len(params) > 0 {
|
||||
dataMap = gconv.Map(params[0])
|
||||
@ -209,7 +114,7 @@ func doCheck(key string, value interface{}, rules string, messages interface{},
|
||||
}
|
||||
} else {
|
||||
// It checks build-in validation rules if there's no custom rule.
|
||||
match, err = doCheckBuildInRules(index, value, ruleKey, rulePattern, ruleItems, data, customMsgMap)
|
||||
match, err = v.doCheckBuildInRules(index, value, ruleKey, rulePattern, ruleItems, data, customMsgMap)
|
||||
if !match && err != nil {
|
||||
errorMsgArray[ruleKey] = err.Error()
|
||||
}
|
||||
@ -220,7 +125,7 @@ func doCheck(key string, value interface{}, rules string, messages interface{},
|
||||
// It does nothing if the error message for this rule
|
||||
// is already set in previous validation.
|
||||
if _, ok := errorMsgArray[ruleKey]; !ok {
|
||||
errorMsgArray[ruleKey] = getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
errorMsgArray[ruleKey] = v.getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
}
|
||||
}
|
||||
index++
|
||||
@ -233,7 +138,7 @@ func doCheck(key string, value interface{}, rules string, messages interface{},
|
||||
return nil
|
||||
}
|
||||
|
||||
func doCheckBuildInRules(
|
||||
func (v *Validator) doCheckBuildInRules(
|
||||
index int,
|
||||
value interface{},
|
||||
ruleKey string,
|
||||
@ -253,7 +158,7 @@ func doCheckBuildInRules(
|
||||
"required-with-all",
|
||||
"required-without",
|
||||
"required-without-all":
|
||||
match = checkRequired(valueStr, ruleKey, rulePattern, dataMap)
|
||||
match = v.checkRequired(valueStr, ruleKey, rulePattern, dataMap)
|
||||
|
||||
// Length rules.
|
||||
// It also supports length of unicode string.
|
||||
@ -261,7 +166,7 @@ func doCheckBuildInRules(
|
||||
"length",
|
||||
"min-length",
|
||||
"max-length":
|
||||
if msg := checkLength(valueStr, ruleKey, rulePattern, customMsgMap); msg != "" {
|
||||
if msg := v.checkLength(valueStr, ruleKey, rulePattern, customMsgMap); msg != "" {
|
||||
return match, errors.New(msg)
|
||||
} else {
|
||||
match = true
|
||||
@ -272,7 +177,7 @@ func doCheckBuildInRules(
|
||||
"min",
|
||||
"max",
|
||||
"between":
|
||||
if msg := checkRange(valueStr, ruleKey, rulePattern, customMsgMap); msg != "" {
|
||||
if msg := v.checkRange(valueStr, ruleKey, rulePattern, customMsgMap); msg != "" {
|
||||
return match, errors.New(msg)
|
||||
} else {
|
||||
match = true
|
||||
@ -308,7 +213,7 @@ func doCheckBuildInRules(
|
||||
match = true
|
||||
} else {
|
||||
var msg string
|
||||
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = v.getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = strings.Replace(msg, ":format", rulePattern, -1)
|
||||
return match, errors.New(msg)
|
||||
}
|
||||
@ -322,7 +227,7 @@ func doCheckBuildInRules(
|
||||
}
|
||||
if !match {
|
||||
var msg string
|
||||
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = v.getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = strings.Replace(msg, ":field", rulePattern, -1)
|
||||
return match, errors.New(msg)
|
||||
}
|
||||
@ -337,7 +242,7 @@ func doCheckBuildInRules(
|
||||
}
|
||||
if !match {
|
||||
var msg string
|
||||
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = v.getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = strings.Replace(msg, ":field", rulePattern, -1)
|
||||
return match, errors.New(msg)
|
||||
}
|
||||
@ -430,11 +335,11 @@ func doCheckBuildInRules(
|
||||
// 总:
|
||||
// (^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)
|
||||
case "resident-id":
|
||||
match = checkResidentId(valueStr)
|
||||
match = v.checkResidentId(valueStr)
|
||||
|
||||
// Bank card number using LUHN algorithm.
|
||||
case "bank-card":
|
||||
match = checkLuHn(valueStr)
|
||||
match = v.checkLuHn(valueStr)
|
||||
|
||||
// Universal passport format rule:
|
||||
// Starting with letter, containing only numbers or underscores, length between 6 and 18.
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -7,9 +7,8 @@
|
||||
package gvalid
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// CheckMap validates map and returns the error result. It returns nil if with successful validation.
|
||||
@ -17,7 +16,7 @@ import (
|
||||
// The parameter <rules> can be type of []string/map[string]string. It supports sequence in error result
|
||||
// if <rules> is type of []string.
|
||||
// The optional parameter <messages> specifies the custom error messages for specified keys and rules.
|
||||
func CheckMap(params interface{}, rules interface{}, messages ...CustomMsg) *Error {
|
||||
func (v *Validator) CheckMap(params interface{}, rules interface{}, messages ...CustomMsg) *Error {
|
||||
// If there's no validation rules, it does nothing and returns quickly.
|
||||
if params == nil || rules == nil {
|
||||
return nil
|
||||
@ -96,7 +95,7 @@ func CheckMap(params interface{}, rules interface{}, messages ...CustomMsg) *Err
|
||||
value = v
|
||||
}
|
||||
// It checks each rule and its value in loop.
|
||||
if e := doCheck(key, value, rule, customMsgs[key], data); e != nil {
|
||||
if e := v.doCheck(key, value, rule, customMsgs[key], data); e != nil {
|
||||
_, item := e.FirstItem()
|
||||
// ===========================================================
|
||||
// Only in map and struct validations, if value is nil or empty
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -7,10 +7,9 @@
|
||||
package gvalid
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/internal/structs"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -24,7 +23,7 @@ var (
|
||||
// The parameter <rules> can be type of []string/map[string]string. It supports sequence in error result
|
||||
// if <rules> is type of []string.
|
||||
// The optional parameter <messages> specifies the custom error messages for specified keys and rules.
|
||||
func CheckStruct(object interface{}, rules interface{}, messages ...CustomMsg) *Error {
|
||||
func (v *Validator) CheckStruct(object interface{}, rules interface{}, messages ...CustomMsg) *Error {
|
||||
// It here must use structs.TagFields not structs.MapField to ensure error sequence.
|
||||
tagField, err := structs.TagFields(object, structTagPriority)
|
||||
if err != nil {
|
||||
@ -166,7 +165,7 @@ func CheckStruct(object interface{}, rules interface{}, messages ...CustomMsg) *
|
||||
value = v
|
||||
}
|
||||
// It checks each rule and its value in loop.
|
||||
if e := doCheck(key, value, rule, customMessage[key], params); e != nil {
|
||||
if e := v.doCheck(key, value, rule, customMessage[key], params); e != nil {
|
||||
_, item := e.FirstItem()
|
||||
// ===========================================================
|
||||
// Only in map and struct validations, if value is nil or empty
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -63,18 +63,18 @@ var defaultMessages = map[string]string{
|
||||
// getErrorMessageByRule retrieves and returns the error message for specified rule.
|
||||
// It firstly retrieves the message from custom message map, and then checks i18n manager,
|
||||
// it returns the default error message if it's not found in custom message map or i18n manager.
|
||||
func getErrorMessageByRule(ruleKey string, customMsgMap map[string]string) string {
|
||||
func (v *Validator) getErrorMessageByRule(ruleKey string, customMsgMap map[string]string) string {
|
||||
content := customMsgMap[ruleKey]
|
||||
if content != "" {
|
||||
return content
|
||||
}
|
||||
content = gi18n.GetContent(fmt.Sprintf(`gf.gvalid.rule.%s`, ruleKey))
|
||||
content = gi18n.GetContent(fmt.Sprintf(`gf.gvalid.rule.%s`, ruleKey), v.i18nLang)
|
||||
if content == "" {
|
||||
content = defaultMessages[ruleKey]
|
||||
}
|
||||
// If there's no configured rule message, it uses default one.
|
||||
if content == "" {
|
||||
content = gi18n.GetContent(`gf.gvalid.rule.__default__`)
|
||||
content = gi18n.GetContent(`gf.gvalid.rule.__default__`, v.i18nLang)
|
||||
if content == "" {
|
||||
content = defaultMessages["__default__"]
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -15,7 +15,7 @@ import (
|
||||
// checkLength checks <value> using length rules.
|
||||
// The length is calculated using unicode string, which means one chinese character or letter
|
||||
// both has the length of 1.
|
||||
func checkLength(value, ruleKey, ruleVal string, customMsgMap map[string]string) string {
|
||||
func (v *Validator) checkLength(value, ruleKey, ruleVal string, customMsgMap map[string]string) string {
|
||||
var (
|
||||
msg = ""
|
||||
runeArray = gconv.Runes(value)
|
||||
@ -39,7 +39,7 @@ func checkLength(value, ruleKey, ruleVal string, customMsgMap map[string]string)
|
||||
}
|
||||
}
|
||||
if valueLen < min || valueLen > max {
|
||||
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = v.getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = strings.Replace(msg, ":min", strconv.Itoa(min), -1)
|
||||
msg = strings.Replace(msg, ":max", strconv.Itoa(max), -1)
|
||||
return msg
|
||||
@ -48,14 +48,14 @@ func checkLength(value, ruleKey, ruleVal string, customMsgMap map[string]string)
|
||||
case "min-length":
|
||||
min, err := strconv.Atoi(ruleVal)
|
||||
if valueLen < min || err != nil {
|
||||
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = v.getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = strings.Replace(msg, ":min", strconv.Itoa(min), -1)
|
||||
}
|
||||
|
||||
case "max-length":
|
||||
max, err := strconv.Atoi(ruleVal)
|
||||
if valueLen > max || err != nil {
|
||||
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = v.getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = strings.Replace(msg, ":max", strconv.Itoa(max), -1)
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -8,7 +8,7 @@ package gvalid
|
||||
|
||||
// checkLuHn checks <value> with LUHN algorithm.
|
||||
// It's usually used for bank card number validation.
|
||||
func checkLuHn(value string) bool {
|
||||
func (v *Validator) checkLuHn(value string) bool {
|
||||
var (
|
||||
sum = 0
|
||||
nDigits = len(value)
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
// checkRange checks <value> using range rules.
|
||||
func checkRange(value, ruleKey, ruleVal string, customMsgMap map[string]string) string {
|
||||
func (v *Validator) checkRange(value, ruleKey, ruleVal string, customMsgMap map[string]string) string {
|
||||
msg := ""
|
||||
switch ruleKey {
|
||||
// Value range.
|
||||
@ -30,9 +30,9 @@ func checkRange(value, ruleKey, ruleVal string, customMsgMap map[string]string)
|
||||
max = v
|
||||
}
|
||||
}
|
||||
v, err := strconv.ParseFloat(value, 10)
|
||||
if v < min || v > max || err != nil {
|
||||
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
valueF, err := strconv.ParseFloat(value, 10)
|
||||
if valueF < min || valueF > max || err != nil {
|
||||
msg = v.getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = strings.Replace(msg, ":min", strconv.FormatFloat(min, 'f', -1, 64), -1)
|
||||
msg = strings.Replace(msg, ":max", strconv.FormatFloat(max, 'f', -1, 64), -1)
|
||||
}
|
||||
@ -44,7 +44,7 @@ func checkRange(value, ruleKey, ruleVal string, customMsgMap map[string]string)
|
||||
valueN, err2 = strconv.ParseFloat(value, 10)
|
||||
)
|
||||
if valueN < min || err1 != nil || err2 != nil {
|
||||
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = v.getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = strings.Replace(msg, ":min", strconv.FormatFloat(min, 'f', -1, 64), -1)
|
||||
}
|
||||
|
||||
@ -55,7 +55,7 @@ func checkRange(value, ruleKey, ruleVal string, customMsgMap map[string]string)
|
||||
valueN, err2 = strconv.ParseFloat(value, 10)
|
||||
)
|
||||
if valueN > max || err1 != nil || err2 != nil {
|
||||
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = v.getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = strings.Replace(msg, ":max", strconv.FormatFloat(max, 'f', -1, 64), -1)
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
// checkRequired checks <value> using required rules.
|
||||
func checkRequired(value, ruleKey, ruleVal string, params map[string]string) bool {
|
||||
func (v *Validator) checkRequired(value, ruleKey, ruleVal string, params map[string]string) bool {
|
||||
required := false
|
||||
switch ruleKey {
|
||||
// Required.
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -32,7 +32,7 @@ import (
|
||||
//
|
||||
// 总:
|
||||
// (^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)
|
||||
func checkResidentId(id string) bool {
|
||||
func (v *Validator) checkResidentId(id string) bool {
|
||||
id = strings.ToUpper(strings.TrimSpace(id))
|
||||
if len(id) != 18 {
|
||||
return false
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2020 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
Reference in New Issue
Block a user