improve validation manager feature for package gvalid

This commit is contained in:
John Guo
2021-05-19 09:25:49 +08:00
parent 0bd1ea07a7
commit ea0340db8e
14 changed files with 352 additions and 344 deletions

View File

@ -18,9 +18,10 @@ import (
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/os/gres"
"github.com/gogf/gf/os/gview"
"github.com/gogf/gf/util/gvalid"
)
// Client is a convenience function, that creates and returns a new HTTP client.
// Client is a convenience function, which creates and returns a new HTTP client.
func Client() *ghttp.Client {
return ghttp.NewClient()
}
@ -110,3 +111,8 @@ func Model(tableNameOrStruct ...interface{}) *gdb.Model {
func Redis(name ...string) *gredis.Redis {
return gins.Redis(name...)
}
// Validator is a convenience function, which creates and returns a new validation manager object.
func Validator() *gvalid.Validator {
return gvalid.New()
}

View File

@ -11,7 +11,7 @@ import (
"github.com/gogf/gf/net/ghttp"
)
// SetEnabled enables/disables the GoFrame internal logging manually.
// SetDebug enables/disables the GoFrame internal logging manually.
// Note that this function is not concurrent safe, be aware of the DATA RACE,
// which means you should call this function in your boot but not the runtime.
func SetDebug(enabled bool) {

View File

@ -102,7 +102,7 @@ func (r *Request) doParse(pointer interface{}, requestType int) error {
}
}
// Validation.
if err := gvalid.CheckStructWithParamMap(r.Context(), pointer, data, nil); err != nil {
if err := gvalid.CheckStructWithData(r.Context(), pointer, data, nil); err != nil {
return err
}
@ -119,7 +119,7 @@ func (r *Request) doParse(pointer interface{}, requestType int) error {
return err
}
for i := 0; i < reflectVal2.Len(); i++ {
if err := gvalid.CheckStructWithParamMap(
if err := gvalid.CheckStructWithData(
r.Context(),
reflectVal2.Index(i),
j.GetMap(gconv.String(i)),

View File

@ -64,15 +64,6 @@ import (
// like: map[field] => string|map[rule]string
type CustomMsg = map[string]interface{}
// doCheckStructWithParamMapInput is used for struct validation for internal function.
type doCheckStructWithParamMapInput struct {
Object interface{} // Can be type of struct/*struct.
ParamMap interface{} // Validation parameter map. Note that it acts different according attribute `UseParamMapInsteadOfObjectValue`.
UseParamMapInsteadOfObjectValue bool // Using `ParamMap` as its validation source instead of values from `Object`.
CustomRules interface{} // Custom validation rules.
CustomErrorMessageMap CustomMsg // Custom error message map for validation rules.
}
// apiNoValidation is an interface that marks current struct not validated by package `gvalid`.
type apiNoValidation interface {
NoValidation()
@ -179,7 +170,7 @@ var (
}
)
// Check checks single value with specified rules.
// CheckValue 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
@ -189,8 +180,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(ctx context.Context, value interface{}, rules string, messages interface{}, params ...interface{}) Error {
return defaultValidator.Ctx(ctx).Check(value, rules, messages, params...)
func CheckValue(ctx context.Context, value interface{}, rules string, messages interface{}, params ...interface{}) Error {
var data interface{}
if len(params) > 0 {
data = params[0]
}
return defaultValidator.Ctx(ctx).Rules(rules).Data(data).Messages(messages).CheckValue(value)
}
// CheckMap validates map and returns the error result. It returns nil if with successful validation.
@ -199,27 +194,39 @@ func Check(ctx context.Context, value interface{}, rules string, messages interf
// if `rules` is type of []string.
// The optional parameter `messages` specifies the custom error messages for specified keys and rules.
func CheckMap(ctx context.Context, params interface{}, rules interface{}, messages ...CustomMsg) Error {
return defaultValidator.Ctx(ctx).CheckMap(params, rules, messages...)
var customErrorMessages CustomMsg
if len(messages) > 0 {
customErrorMessages = messages[0]
}
return defaultValidator.Ctx(ctx).Rules(rules).Messages(customErrorMessages).CheckMap(params)
}
// CheckStruct validates strcut and returns the error result.
// CheckStruct validates struct 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(ctx context.Context, object interface{}, rules interface{}, messages ...CustomMsg) Error {
return defaultValidator.Ctx(ctx).CheckStruct(object, rules, messages...)
var customErrorMessages CustomMsg
if len(messages) > 0 {
customErrorMessages = messages[0]
}
return defaultValidator.Ctx(ctx).Rules(rules).Messages(customErrorMessages).CheckStruct(object)
}
// CheckStructWithParamMap validates struct with given parameter map and returns the error result.
// CheckStructWithData validates struct with given parameter map 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 CheckStructWithParamMap(ctx context.Context, object interface{}, paramMap interface{}, rules interface{}, messages ...CustomMsg) Error {
return defaultValidator.Ctx(ctx).CheckStructWithParamMap(object, paramMap, rules, messages...)
func CheckStructWithData(ctx context.Context, object interface{}, data interface{}, rules interface{}, messages ...CustomMsg) Error {
var customErrorMessages CustomMsg
if len(messages) > 0 {
customErrorMessages = messages[0]
}
return defaultValidator.Ctx(ctx).Data(data).Rules(rules).Messages(customErrorMessages).CheckStruct(object)
}
// parseSequenceTag parses one sequence tag to field, rule and error message.

View File

@ -11,11 +11,16 @@ import (
"github.com/gogf/gf/i18n/gi18n"
)
// Validator is the validation manager.
// Validator is the validation manager for chaining operations.
type Validator struct {
ctx context.Context // Context containing custom context variables.
i18nManager *gi18n.Manager // I18n manager for error message translation.
ctx context.Context // Context containing custom context variables.
i18nManager *gi18n.Manager // I18n manager for error message translation.
key string // Single validation key.
value interface{} // Single validation value.
data interface{} // Validation data, which is usually a map.
rules interface{} // Custom validation data.
messages interface{} // Custom validation error messages, which can be string or type of CustomMsg.
useDataInsteadOfObjectAttributes bool // Using `data` as its validation source instead of attribute values from `Object`.
}
// New creates and returns a new Validator.
@ -26,14 +31,49 @@ func New() *Validator {
}
}
// I18n sets the i18n manager for the validator.
func (v *Validator) I18n(i18nManager *gi18n.Manager) *Validator {
v.i18nManager = i18nManager
return v
// 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
}
// Ctx is a chaining operation function which sets the context for next validation.
func (v *Validator) Ctx(ctx context.Context) *Validator {
v.ctx = ctx
return v
// I18n sets the i18n manager for the validator.
func (v *Validator) I18n(i18nManager *gi18n.Manager) *Validator {
newValidator := v.Clone()
newValidator.i18nManager = i18nManager
return newValidator
}
// Ctx is a chaining operation function, which sets the context for next validation.
func (v *Validator) Ctx(ctx context.Context) *Validator {
newValidator := v.Clone()
newValidator.ctx = ctx
return newValidator
}
// Data is a chaining operation function, which sets validation data for current operation.
// The parameter `data` usually be type of map, which specifies the parameter map used in validation.
// Calling this function also sets `useDataInsteadOfObjectAttributes` true no mather the `data` is nil or not.
func (v *Validator) Data(data interface{}) *Validator {
newValidator := v.Clone()
newValidator.data = data
newValidator.useDataInsteadOfObjectAttributes = true
return newValidator
}
// Rules is a chaining operation function, which sets custom validation rules for current operation.
func (v *Validator) Rules(rules interface{}) *Validator {
newValidator := v.Clone()
newValidator.rules = rules
return newValidator
}
// Messages is a chaining operation function, which sets custom error messages for current operation.
// The parameter `messages` can be type of string/[]string/map[string]string. It supports sequence in error result
// if `rules` is type of []string.
func (v *Validator) Messages(messages interface{}) *Validator {
newValidator := v.Clone()
newValidator.messages = messages
return newValidator
}

View File

@ -25,22 +25,14 @@ type apiTime interface {
IsZero() bool
}
// Check checks single value with specified rules.
// CheckValue 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 (v *Validator) Check(value interface{}, rules string, messages interface{}, paramMap ...interface{}) Error {
return v.doCheck("", value, rules, messages, paramMap...)
func (v *Validator) CheckValue(value interface{}) Error {
return v.doCheckValue("", value, gconv.String(v.rules), v.messages, v.data)
}
// doCheck does the really rules validation for single key-value.
func (v *Validator) doCheck(key string, value interface{}, rules string, messages interface{}, paramMap ...interface{}) Error {
// doCheckSingleValue does the really rules validation for single key-value.
func (v *Validator) doCheckValue(key string, value interface{}, rules string, messages interface{}, paramMap ...interface{}) Error {
// If there's no validation rules, it does nothing and returns quickly.
if rules == "" {
return nil
@ -51,7 +43,7 @@ func (v *Validator) doCheck(key string, value interface{}, rules string, message
data = make(map[string]interface{})
errorMsgArray = make(map[string]string)
)
if len(paramMap) > 0 {
if len(paramMap) > 0 && paramMap[0] != nil {
data = gconv.Map(paramMap[0])
}
// Custom error messages handling.
@ -108,7 +100,7 @@ func (v *Validator) doCheck(key string, value interface{}, rules string, message
dataMap map[string]interface{}
message = v.getErrorMessageByRule(ruleKey, customMsgMap)
)
if len(paramMap) > 0 {
if len(paramMap) > 0 && paramMap[0] != nil {
dataMap = gconv.Map(paramMap[0])
}
if err := f(ruleItems[index], value, message, dataMap); err != nil {

View File

@ -12,13 +12,14 @@ import (
)
// 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 (v *Validator) CheckMap(params interface{}, rules interface{}, messages ...CustomMsg) Error {
// The parameter `params` should be type of map.
func (v *Validator) CheckMap(params interface{}) Error {
return v.doCheckMap(params)
}
func (v *Validator) doCheckMap(params interface{}) Error {
// If there's no validation rules, it does nothing and returns quickly.
if params == nil || rules == nil {
if params == nil || v.rules == nil {
return nil
}
var (
@ -27,7 +28,7 @@ func (v *Validator) CheckMap(params interface{}, rules interface{}, messages ...
errorRules = make([]string, 0)
errorMaps = make(map[string]map[string]string)
)
switch v := rules.(type) {
switch v := v.rules.(type) {
// Sequence tag: []sequence tag
// Sequence has order for error results.
case []string:
@ -76,13 +77,13 @@ func (v *Validator) CheckMap(params interface{}, rules interface{}, messages ...
"invalid params type: convert to map failed",
)
}
if len(messages) > 0 && len(messages[0]) > 0 {
if msg, ok := v.messages.(CustomMsg); ok && len(msg) > 0 {
if len(customMsgs) > 0 {
for k, v := range messages[0] {
for k, v := range msg {
customMsgs[k] = v
}
} else {
customMsgs = messages[0]
customMsgs = msg
}
}
var value interface{}
@ -95,7 +96,7 @@ func (v *Validator) CheckMap(params interface{}, rules interface{}, messages ...
value = v
}
// It checks each rule and its value in loop.
if e := v.doCheck(key, value, rule, customMsgs[key], data); e != nil {
if e := v.doCheckValue(key, value, rule, customMsgs[key], data); e != nil {
_, item := e.FirstItem()
// ===========================================================
// Only in map and struct validations, if value is nil or empty

View File

@ -14,52 +14,17 @@ import (
)
// CheckStruct validates struct and returns the error result.
//
// The parameter `object` should be type of struct/*struct.
// The parameter `customRules` can be type of []string/map[string]string. It supports sequence in error result
// if `rules` is type of []string.
// The optional parameter `customErrorMessageMap` specifies the custom error messages for specified keys and rules.
func (v *Validator) CheckStruct(object interface{}, customRules interface{}, customErrorMessageMap ...CustomMsg) Error {
var message CustomMsg
if len(customErrorMessageMap) > 0 {
message = customErrorMessageMap[0]
}
return v.doCheckStructWithParamMap(&doCheckStructWithParamMapInput{
Object: object,
ParamMap: nil,
UseParamMapInsteadOfObjectValue: false,
CustomRules: customRules,
CustomErrorMessageMap: message,
})
func (v *Validator) CheckStruct(object interface{}) Error {
return v.doCheckStruct(object)
}
// CheckStructWithParamMap validates struct with given parameter map and returns the error result.
//
// The parameter `object` should be type of struct/*struct.
// The parameter `paramMap` should be type of map, which specifies the parameter map used in validation.
// The parameter `customRules` can be type of []string/map[string]string. It supports sequence in error result
// if `rules` is type of []string.
// The optional parameter `customErrorMessageMap` specifies the custom error messages for specified keys and rules.
func (v *Validator) CheckStructWithParamMap(object interface{}, paramMap interface{}, customRules interface{}, customErrorMessageMap ...CustomMsg) Error {
var message CustomMsg
if len(customErrorMessageMap) > 0 {
message = customErrorMessageMap[0]
}
return v.doCheckStructWithParamMap(&doCheckStructWithParamMapInput{
Object: object,
ParamMap: paramMap,
UseParamMapInsteadOfObjectValue: true,
CustomRules: customRules,
CustomErrorMessageMap: message,
})
}
func (v *Validator) doCheckStructWithParamMap(input *doCheckStructWithParamMapInput) Error {
func (v *Validator) doCheckStruct(object interface{}) Error {
var (
// Returning error.
errorMaps = make(map[string]map[string]string)
)
fieldMap, err := structs.FieldMap(input.Object, aliasNameTagPriority, true)
fieldMap, err := structs.FieldMap(object, aliasNameTagPriority, true)
if err != nil {
return newErrorStr("invalid_object", err.Error())
}
@ -73,10 +38,7 @@ func (v *Validator) doCheckStructWithParamMap(input *doCheckStructWithParamMapIn
if _, ok := field.TagLookup(noValidationTagName); ok {
continue
}
recursiveInput := doCheckStructWithParamMapInput{}
recursiveInput = *input
recursiveInput.Object = field.Value
if err := v.doCheckStructWithParamMap(&recursiveInput); err != nil {
if err := v.doCheckStruct(field.Value); err != nil {
// It merges the errors into single error map.
for k, m := range err.(*validationError).errors {
errorMaps[k] = m
@ -85,12 +47,12 @@ func (v *Validator) doCheckStructWithParamMap(input *doCheckStructWithParamMapIn
}
}
// It here must use structs.TagFields not structs.FieldMap to ensure error sequence.
tagField, err := structs.TagFields(input.Object, structTagPriority)
tagField, err := structs.TagFields(object, structTagPriority)
if err != nil {
return newErrorStr("invalid_object", err.Error())
}
// If there's no struct tag and validation rules, it does nothing and returns quickly.
if len(tagField) == 0 && input.CustomRules == nil {
if len(tagField) == 0 && v.messages == nil {
return nil
}
@ -101,7 +63,7 @@ func (v *Validator) doCheckStructWithParamMap(input *doCheckStructWithParamMapIn
fieldAliases = make(map[string]string) // Alias names for `messages` overwriting struct tag names.
errorRules = make([]string, 0) // Sequence rules.
)
switch v := input.CustomRules.(type) {
switch v := v.rules.(type) {
// Sequence tag: []sequence tag
// Sequence has order for error results.
case []string:
@ -145,13 +107,13 @@ func (v *Validator) doCheckStructWithParamMap(input *doCheckStructWithParamMapIn
return nil
}
// Input parameter map handling.
if input.ParamMap == nil || !input.UseParamMapInsteadOfObjectValue {
if v.data == nil || !v.useDataInsteadOfObjectAttributes {
inputParamMap = make(map[string]interface{})
} else {
inputParamMap = gconv.Map(input.ParamMap)
inputParamMap = gconv.Map(v.data)
}
// Checks and extends the parameters map with struct alias tag.
if !input.UseParamMapInsteadOfObjectValue {
if !v.useDataInsteadOfObjectAttributes {
for nameOrTag, field := range fieldMap {
inputParamMap[nameOrTag] = field.Value.Interface()
if nameOrTag != field.Name() {
@ -173,7 +135,7 @@ func (v *Validator) doCheckStructWithParamMap(input *doCheckStructWithParamMapIn
}
// It here extends the params map using alias names.
if _, ok := inputParamMap[name]; !ok {
if !input.UseParamMapInsteadOfObjectValue {
if !v.useDataInsteadOfObjectAttributes {
inputParamMap[name] = field.Value.Interface()
}
}
@ -216,8 +178,8 @@ func (v *Validator) doCheckStructWithParamMap(input *doCheckStructWithParamMapIn
// Custom error messages,
// which have the most priority than `rules` and struct tag.
if len(input.CustomErrorMessageMap) > 0 {
for k, v := range input.CustomErrorMessageMap {
if msg, ok := v.messages.(CustomMsg); ok && len(msg) > 0 {
for k, v := range msg {
if a, ok := fieldAliases[k]; ok {
// Overwrite the key of field name.
customMessage[a] = v
@ -232,7 +194,7 @@ func (v *Validator) doCheckStructWithParamMap(input *doCheckStructWithParamMapIn
for key, rule := range checkRules {
_, value = gutil.MapPossibleItemByKey(inputParamMap, key)
// It checks each rule and its value in loop.
if e := v.doCheck(key, value, rule, customMessage[key], inputParamMap); e != nil {
if e := v.doCheckValue(key, value, rule, customMessage[key], inputParamMap); e != nil {
_, item := e.FirstItem()
// ===================================================================
// Only in map and struct validations, if value is nil or empty string

View File

@ -173,14 +173,14 @@ func ExampleRegisterRule_OverwriteRequired() {
}
return nil
})
fmt.Println(gvalid.Check(context.TODO(), "", "required", "It's required"))
fmt.Println(gvalid.Check(context.TODO(), 0, "required", "It's required"))
fmt.Println(gvalid.Check(context.TODO(), false, "required", "It's required"))
fmt.Println(gvalid.CheckValue(context.TODO(), "", "required", "It's required"))
fmt.Println(gvalid.CheckValue(context.TODO(), 0, "required", "It's required"))
fmt.Println(gvalid.CheckValue(context.TODO(), false, "required", "It's required"))
gvalid.DeleteRule(rule)
fmt.Println("rule deleted")
fmt.Println(gvalid.Check(context.TODO(), "", "required", "It's required"))
fmt.Println(gvalid.Check(context.TODO(), 0, "required", "It's required"))
fmt.Println(gvalid.Check(context.TODO(), false, "required", "It's required"))
fmt.Println(gvalid.CheckValue(context.TODO(), "", "required", "It's required"))
fmt.Println(gvalid.CheckValue(context.TODO(), 0, "required", "It's required"))
fmt.Println(gvalid.CheckValue(context.TODO(), false, "required", "It's required"))
// Output:
// It's required
// It's required

View File

@ -24,9 +24,9 @@ func Test_Check(t *testing.T) {
val1 := 0
val2 := 7
val3 := 20
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
t.Assert(err1, "invalid_rules: abc:6,16")
t.Assert(err2, "invalid_rules: abc:6,16")
t.Assert(err3, "invalid_rules: abc:6,16")
@ -34,16 +34,16 @@ func Test_Check(t *testing.T) {
}
func Test_Required(t *testing.T) {
if m := gvalid.Check(context.TODO(), "1", "required", nil); m != nil {
if m := gvalid.CheckValue(context.TODO(), "1", "required", nil); m != nil {
t.Error(m)
}
if m := gvalid.Check(context.TODO(), "", "required", nil); m == nil {
if m := gvalid.CheckValue(context.TODO(), "", "required", nil); m == nil {
t.Error(m)
}
if m := gvalid.Check(context.TODO(), "", "required-if: id,1,age,18", nil, map[string]interface{}{"id": 1, "age": 19}); m == nil {
if m := gvalid.CheckValue(context.TODO(), "", "required-if: id,1,age,18", nil, map[string]interface{}{"id": 1, "age": 19}); m == nil {
t.Error("Required校验失败")
}
if m := gvalid.Check(context.TODO(), "", "required-if: id,1,age,18", nil, map[string]interface{}{"id": 2, "age": 19}); m != nil {
if m := gvalid.CheckValue(context.TODO(), "", "required-if: id,1,age,18", nil, map[string]interface{}{"id": 2, "age": 19}); m != nil {
t.Error("Required校验失败")
}
}
@ -51,20 +51,20 @@ func Test_Required(t *testing.T) {
func Test_RequiredIf(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
rule := "required-if:id,1,age,18"
t.AssertNE(gvalid.Check(context.TODO(), "", rule, nil, g.Map{"id": 1}), nil)
t.Assert(gvalid.Check(context.TODO(), "", rule, nil, g.Map{"id": 0}), nil)
t.AssertNE(gvalid.Check(context.TODO(), "", rule, nil, g.Map{"age": 18}), nil)
t.Assert(gvalid.Check(context.TODO(), "", rule, nil, g.Map{"age": 20}), nil)
t.AssertNE(gvalid.CheckValue(context.TODO(), "", rule, nil, g.Map{"id": 1}), nil)
t.Assert(gvalid.CheckValue(context.TODO(), "", rule, nil, g.Map{"id": 0}), nil)
t.AssertNE(gvalid.CheckValue(context.TODO(), "", rule, nil, g.Map{"age": 18}), nil)
t.Assert(gvalid.CheckValue(context.TODO(), "", rule, nil, g.Map{"age": 20}), nil)
})
}
func Test_RequiredUnless(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
rule := "required-unless:id,1,age,18"
t.Assert(gvalid.Check(context.TODO(), "", rule, nil, g.Map{"id": 1}), nil)
t.AssertNE(gvalid.Check(context.TODO(), "", rule, nil, g.Map{"id": 0}), nil)
t.Assert(gvalid.Check(context.TODO(), "", rule, nil, g.Map{"age": 18}), nil)
t.AssertNE(gvalid.Check(context.TODO(), "", rule, nil, g.Map{"age": 20}), nil)
t.Assert(gvalid.CheckValue(context.TODO(), "", rule, nil, g.Map{"id": 1}), nil)
t.AssertNE(gvalid.CheckValue(context.TODO(), "", rule, nil, g.Map{"id": 0}), nil)
t.Assert(gvalid.CheckValue(context.TODO(), "", rule, nil, g.Map{"age": 18}), nil)
t.AssertNE(gvalid.CheckValue(context.TODO(), "", rule, nil, g.Map{"age": 20}), nil)
})
}
@ -82,9 +82,9 @@ func Test_RequiredWith(t *testing.T) {
"id": 100,
"name": "john",
}
err1 := gvalid.Check(context.TODO(), val1, rule, nil, params1)
err2 := gvalid.Check(context.TODO(), val1, rule, nil, params2)
err3 := gvalid.Check(context.TODO(), val1, rule, nil, params3)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params1)
err2 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params2)
err3 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params3)
t.Assert(err1, nil)
t.AssertNE(err2, nil)
t.AssertNE(err3, nil)
@ -102,9 +102,9 @@ func Test_RequiredWith(t *testing.T) {
params3 := g.Map{
"time": time.Time{},
}
err1 := gvalid.Check(context.TODO(), val1, rule, nil, params1)
err2 := gvalid.Check(context.TODO(), val1, rule, nil, params2)
err3 := gvalid.Check(context.TODO(), val1, rule, nil, params3)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params1)
err2 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params2)
err3 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params3)
t.Assert(err1, nil)
t.AssertNE(err2, nil)
t.Assert(err3, nil)
@ -121,9 +121,9 @@ func Test_RequiredWith(t *testing.T) {
params3 := g.Map{
"time": time.Now(),
}
err1 := gvalid.Check(context.TODO(), val1, rule, nil, params1)
err2 := gvalid.Check(context.TODO(), val1, rule, nil, params2)
err3 := gvalid.Check(context.TODO(), val1, rule, nil, params3)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params1)
err2 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params2)
err3 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params3)
t.Assert(err1, nil)
t.AssertNE(err2, nil)
t.AssertNE(err3, nil)
@ -171,9 +171,9 @@ func Test_RequiredWithAll(t *testing.T) {
"id": 100,
"name": "john",
}
err1 := gvalid.Check(context.TODO(), val1, rule, nil, params1)
err2 := gvalid.Check(context.TODO(), val1, rule, nil, params2)
err3 := gvalid.Check(context.TODO(), val1, rule, nil, params3)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params1)
err2 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params2)
err3 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params3)
t.Assert(err1, nil)
t.Assert(err2, nil)
t.AssertNE(err3, nil)
@ -194,9 +194,9 @@ func Test_RequiredWithOut(t *testing.T) {
"id": 100,
"name": "john",
}
err1 := gvalid.Check(context.TODO(), val1, rule, nil, params1)
err2 := gvalid.Check(context.TODO(), val1, rule, nil, params2)
err3 := gvalid.Check(context.TODO(), val1, rule, nil, params3)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params1)
err2 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params2)
err3 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params3)
t.AssertNE(err1, nil)
t.AssertNE(err2, nil)
t.Assert(err3, nil)
@ -217,9 +217,9 @@ func Test_RequiredWithOutAll(t *testing.T) {
"id": 100,
"name": "john",
}
err1 := gvalid.Check(context.TODO(), val1, rule, nil, params1)
err2 := gvalid.Check(context.TODO(), val1, rule, nil, params2)
err3 := gvalid.Check(context.TODO(), val1, rule, nil, params3)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params1)
err2 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params2)
err3 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params3)
t.AssertNE(err1, nil)
t.Assert(err2, nil)
t.Assert(err3, nil)
@ -236,13 +236,13 @@ func Test_Date(t *testing.T) {
val5 := "2010.11.01"
val6 := "2010/11/01"
val7 := "2010=11=01"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err5 := gvalid.Check(context.TODO(), val5, rule, nil)
err6 := gvalid.Check(context.TODO(), val6, rule, nil)
err7 := gvalid.Check(context.TODO(), val7, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil)
err6 := gvalid.CheckValue(context.TODO(), val6, rule, nil)
err7 := gvalid.CheckValue(context.TODO(), val7, rule, nil)
t.Assert(err1, nil)
t.Assert(err2, nil)
t.Assert(err3, nil)
@ -261,12 +261,12 @@ func Test_DateFormat(t *testing.T) {
val4 := "201011-01"
val5 := "2010~11~01"
val6 := "2010-11~01"
err1 := gvalid.Check(context.TODO(), val1, "date-format:Y", nil)
err2 := gvalid.Check(context.TODO(), val2, "date-format:Ym", nil)
err3 := gvalid.Check(context.TODO(), val3, "date-format:Y.m", nil)
err4 := gvalid.Check(context.TODO(), val4, "date-format:Ym-d", nil)
err5 := gvalid.Check(context.TODO(), val5, "date-format:Y~m~d", nil)
err6 := gvalid.Check(context.TODO(), val6, "date-format:Y~m~d", nil)
err1 := gvalid.CheckValue(context.TODO(), val1, "date-format:Y", nil)
err2 := gvalid.CheckValue(context.TODO(), val2, "date-format:Ym", nil)
err3 := gvalid.CheckValue(context.TODO(), val3, "date-format:Y.m", nil)
err4 := gvalid.CheckValue(context.TODO(), val4, "date-format:Ym-d", nil)
err5 := gvalid.CheckValue(context.TODO(), val5, "date-format:Y~m~d", nil)
err6 := gvalid.CheckValue(context.TODO(), val6, "date-format:Y~m~d", nil)
t.Assert(err1, nil)
t.Assert(err2, nil)
t.Assert(err3, nil)
@ -277,8 +277,8 @@ func Test_DateFormat(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t1 := gtime.Now()
t2 := time.Time{}
err1 := gvalid.Check(context.TODO(), t1, "date-format:Y", nil)
err2 := gvalid.Check(context.TODO(), t2, "date-format:Y", nil)
err1 := gvalid.CheckValue(context.TODO(), t1, "date-format:Y", nil)
err2 := gvalid.CheckValue(context.TODO(), t2, "date-format:Y", nil)
t.Assert(err1, nil)
t.AssertNE(err2, nil)
})
@ -291,10 +291,10 @@ func Test_Email(t *testing.T) {
value2 := "m@www@johngcn"
value3 := "m-m_m@mail.johng.cn"
value4 := "m.m-m@johng.cn"
err1 := gvalid.Check(context.TODO(), value1, rule, nil)
err2 := gvalid.Check(context.TODO(), value2, rule, nil)
err3 := gvalid.Check(context.TODO(), value3, rule, nil)
err4 := gvalid.Check(context.TODO(), value4, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), value1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), value2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), value3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), value4, rule, nil)
t.AssertNE(err1, nil)
t.AssertNE(err2, nil)
t.Assert(err3, nil)
@ -304,10 +304,10 @@ func Test_Email(t *testing.T) {
func Test_Phone(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
err1 := gvalid.Check(context.TODO(), "1361990897", "phone", nil)
err2 := gvalid.Check(context.TODO(), "13619908979", "phone", nil)
err3 := gvalid.Check(context.TODO(), "16719908979", "phone", nil)
err4 := gvalid.Check(context.TODO(), "19719908989", "phone", nil)
err1 := gvalid.CheckValue(context.TODO(), "1361990897", "phone", nil)
err2 := gvalid.CheckValue(context.TODO(), "13619908979", "phone", nil)
err3 := gvalid.CheckValue(context.TODO(), "16719908979", "phone", nil)
err4 := gvalid.CheckValue(context.TODO(), "19719908989", "phone", nil)
t.AssertNE(err1.String(), nil)
t.Assert(err2, nil)
t.Assert(err3, nil)
@ -317,12 +317,12 @@ func Test_Phone(t *testing.T) {
func Test_PhoneLoose(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
err1 := gvalid.Check(context.TODO(), "13333333333", "phone-loose", nil)
err2 := gvalid.Check(context.TODO(), "15555555555", "phone-loose", nil)
err3 := gvalid.Check(context.TODO(), "16666666666", "phone-loose", nil)
err4 := gvalid.Check(context.TODO(), "23333333333", "phone-loose", nil)
err5 := gvalid.Check(context.TODO(), "1333333333", "phone-loose", nil)
err6 := gvalid.Check(context.TODO(), "10333333333", "phone-loose", nil)
err1 := gvalid.CheckValue(context.TODO(), "13333333333", "phone-loose", nil)
err2 := gvalid.CheckValue(context.TODO(), "15555555555", "phone-loose", nil)
err3 := gvalid.CheckValue(context.TODO(), "16666666666", "phone-loose", nil)
err4 := gvalid.CheckValue(context.TODO(), "23333333333", "phone-loose", nil)
err5 := gvalid.CheckValue(context.TODO(), "1333333333", "phone-loose", nil)
err6 := gvalid.CheckValue(context.TODO(), "10333333333", "phone-loose", nil)
t.Assert(err1, nil)
t.Assert(err2, nil)
t.Assert(err3, nil)
@ -339,11 +339,11 @@ func Test_Telephone(t *testing.T) {
val3 := "86292651"
val4 := "028-8692651"
val5 := "0830-8692651"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err5 := gvalid.Check(context.TODO(), val5, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil)
t.AssertNE(err1, nil)
t.AssertNE(err2, nil)
t.Assert(err3, nil)
@ -360,11 +360,11 @@ func Test_Passport(t *testing.T) {
val3 := "aaaaa"
val4 := "aaaaaa"
val5 := "a123_456"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err5 := gvalid.Check(context.TODO(), val5, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil)
t.AssertNE(err1, nil)
t.AssertNE(err2, nil)
t.AssertNE(err3, nil)
@ -381,11 +381,11 @@ func Test_Password(t *testing.T) {
val3 := "a12345-6"
val4 := ">,/;'[09-"
val5 := "a123_456"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err5 := gvalid.Check(context.TODO(), val5, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil)
t.AssertNE(err1, nil)
t.AssertNE(err2, nil)
t.Assert(err3, nil)
@ -404,13 +404,13 @@ func Test_Password2(t *testing.T) {
val5 := "a123_456"
val6 := "Nant1986"
val7 := "Nant1986!"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err5 := gvalid.Check(context.TODO(), val5, rule, nil)
err6 := gvalid.Check(context.TODO(), val6, rule, nil)
err7 := gvalid.Check(context.TODO(), val7, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil)
err6 := gvalid.CheckValue(context.TODO(), val6, rule, nil)
err7 := gvalid.CheckValue(context.TODO(), val7, rule, nil)
t.AssertNE(err1, nil)
t.AssertNE(err2, nil)
t.AssertNE(err3, nil)
@ -431,13 +431,13 @@ func Test_Password3(t *testing.T) {
val5 := "a123_456"
val6 := "Nant1986"
val7 := "Nant1986!"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err5 := gvalid.Check(context.TODO(), val5, rule, nil)
err6 := gvalid.Check(context.TODO(), val6, rule, nil)
err7 := gvalid.Check(context.TODO(), val7, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil)
err6 := gvalid.CheckValue(context.TODO(), val6, rule, nil)
err7 := gvalid.CheckValue(context.TODO(), val7, rule, nil)
t.AssertNE(err1, nil)
t.AssertNE(err2, nil)
t.AssertNE(err3, nil)
@ -453,8 +453,8 @@ func Test_Postcode(t *testing.T) {
rule := "postcode"
val1 := "12345"
val2 := "610036"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
t.AssertNE(err1, nil)
t.Assert(err2, nil)
})
@ -468,11 +468,11 @@ func Test_ResidentId(t *testing.T) {
val3 := "311128500121201"
val4 := "510521198607185367"
val5 := "51052119860718536x"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err5 := gvalid.Check(context.TODO(), val5, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil)
t.AssertNE(err1, nil)
t.AssertNE(err2, nil)
t.AssertNE(err3, nil)
@ -486,8 +486,8 @@ func Test_BankCard(t *testing.T) {
rule := "bank-card"
val1 := "6230514630000424470"
val2 := "6230514630000424473"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
t.AssertNE(err1, nil)
t.Assert(err2, nil)
})
@ -501,11 +501,11 @@ func Test_QQ(t *testing.T) {
val3 := "10000"
val4 := "38996181"
val5 := "389961817"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err5 := gvalid.Check(context.TODO(), val5, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil)
t.AssertNE(err1, nil)
t.AssertNE(err2, nil)
t.Assert(err3, nil)
@ -515,31 +515,31 @@ func Test_QQ(t *testing.T) {
}
func Test_Ip(t *testing.T) {
if m := gvalid.Check(context.TODO(), "10.0.0.1", "ip", nil); m != nil {
if m := gvalid.CheckValue(context.TODO(), "10.0.0.1", "ip", nil); m != nil {
t.Error(m)
}
if m := gvalid.Check(context.TODO(), "10.0.0.1", "ipv4", nil); m != nil {
if m := gvalid.CheckValue(context.TODO(), "10.0.0.1", "ipv4", nil); m != nil {
t.Error(m)
}
if m := gvalid.Check(context.TODO(), "0.0.0.0", "ipv4", nil); m != nil {
if m := gvalid.CheckValue(context.TODO(), "0.0.0.0", "ipv4", nil); m != nil {
t.Error(m)
}
if m := gvalid.Check(context.TODO(), "1920.0.0.0", "ipv4", nil); m == nil {
if m := gvalid.CheckValue(context.TODO(), "1920.0.0.0", "ipv4", nil); m == nil {
t.Error("ipv4校验失败")
}
if m := gvalid.Check(context.TODO(), "1920.0.0.0", "ip", nil); m == nil {
if m := gvalid.CheckValue(context.TODO(), "1920.0.0.0", "ip", nil); m == nil {
t.Error("ipv4校验失败")
}
if m := gvalid.Check(context.TODO(), "fe80::5484:7aff:fefe:9799", "ipv6", nil); m != nil {
if m := gvalid.CheckValue(context.TODO(), "fe80::5484:7aff:fefe:9799", "ipv6", nil); m != nil {
t.Error(m)
}
if m := gvalid.Check(context.TODO(), "fe80::5484:7aff:fefe:9799123", "ipv6", nil); m == nil {
if m := gvalid.CheckValue(context.TODO(), "fe80::5484:7aff:fefe:9799123", "ipv6", nil); m == nil {
t.Error(m)
}
if m := gvalid.Check(context.TODO(), "fe80::5484:7aff:fefe:9799", "ip", nil); m != nil {
if m := gvalid.CheckValue(context.TODO(), "fe80::5484:7aff:fefe:9799", "ip", nil); m != nil {
t.Error(m)
}
if m := gvalid.Check(context.TODO(), "fe80::5484:7aff:fefe:9799123", "ip", nil); m == nil {
if m := gvalid.CheckValue(context.TODO(), "fe80::5484:7aff:fefe:9799123", "ip", nil); m == nil {
t.Error(m)
}
}
@ -552,11 +552,11 @@ func Test_IPv4(t *testing.T) {
val3 := "1.1.1.1"
val4 := "255.255.255.0"
val5 := "127.0.0.1"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err5 := gvalid.Check(context.TODO(), val5, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil)
t.AssertNE(err1, nil)
t.Assert(err2, nil)
t.Assert(err3, nil)
@ -573,11 +573,11 @@ func Test_IPv6(t *testing.T) {
val3 := "1030::C9B4:FF12:48AA:1A2B"
val4 := "2000:0:0:0:0:0:0:1"
val5 := "0000:0000:0000:0000:0000:ffff:c0a8:5909"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err5 := gvalid.Check(context.TODO(), val5, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil)
t.AssertNE(err1, nil)
t.Assert(err2, nil)
t.Assert(err3, nil)
@ -592,9 +592,9 @@ func Test_MAC(t *testing.T) {
val1 := "192.168.1.1"
val2 := "44-45-53-54-00-00"
val3 := "01:00:5e:00:00:00"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
t.AssertNE(err1, nil)
t.Assert(err2, nil)
t.Assert(err3, nil)
@ -608,10 +608,10 @@ func Test_URL(t *testing.T) {
val2 := "https://www.baidu.com"
val3 := "http://127.0.0.1"
val4 := "file:///tmp/test.txt"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
t.AssertNE(err1, nil)
t.Assert(err2, nil)
t.Assert(err3, nil)
@ -638,7 +638,7 @@ func Test_Domain(t *testing.T) {
}
var err error
for k, v := range m {
err = gvalid.Check(context.TODO(), k, "domain", nil)
err = gvalid.CheckValue(context.TODO(), k, "domain", nil)
if v {
//fmt.Println(k)
t.Assert(err, nil)
@ -652,10 +652,10 @@ func Test_Domain(t *testing.T) {
func Test_Length(t *testing.T) {
rule := "length:6,16"
if m := gvalid.Check(context.TODO(), "123456", rule, nil); m != nil {
if m := gvalid.CheckValue(context.TODO(), "123456", rule, nil); m != nil {
t.Error(m)
}
if m := gvalid.Check(context.TODO(), "12345", rule, nil); m == nil {
if m := gvalid.CheckValue(context.TODO(), "12345", rule, nil); m == nil {
t.Error("长度校验失败")
}
}
@ -665,18 +665,18 @@ func Test_MinLength(t *testing.T) {
msgs := map[string]string{
"min-length": "地址长度至少为:min位",
}
if m := gvalid.Check(context.TODO(), "123456", rule, nil); m != nil {
if m := gvalid.CheckValue(context.TODO(), "123456", rule, nil); m != nil {
t.Error(m)
}
if m := gvalid.Check(context.TODO(), "12345", rule, nil); m == nil {
if m := gvalid.CheckValue(context.TODO(), "12345", rule, nil); m == nil {
t.Error("长度校验失败")
}
if m := gvalid.Check(context.TODO(), "12345", rule, msgs); m == nil {
if m := gvalid.CheckValue(context.TODO(), "12345", rule, msgs); m == nil {
t.Error("长度校验失败")
}
rule2 := "min-length:abc"
if m := gvalid.Check(context.TODO(), "123456", rule2, nil); m == nil {
if m := gvalid.CheckValue(context.TODO(), "123456", rule2, nil); m == nil {
t.Error("长度校验失败")
}
}
@ -686,31 +686,31 @@ func Test_MaxLength(t *testing.T) {
msgs := map[string]string{
"max-length": "地址长度至大为:max位",
}
if m := gvalid.Check(context.TODO(), "12345", rule, nil); m != nil {
if m := gvalid.CheckValue(context.TODO(), "12345", rule, nil); m != nil {
t.Error(m)
}
if m := gvalid.Check(context.TODO(), "1234567", rule, nil); m == nil {
if m := gvalid.CheckValue(context.TODO(), "1234567", rule, nil); m == nil {
t.Error("长度校验失败")
}
if m := gvalid.Check(context.TODO(), "1234567", rule, msgs); m == nil {
if m := gvalid.CheckValue(context.TODO(), "1234567", rule, msgs); m == nil {
t.Error("长度校验失败")
}
rule2 := "max-length:abc"
if m := gvalid.Check(context.TODO(), "123456", rule2, nil); m == nil {
if m := gvalid.CheckValue(context.TODO(), "123456", rule2, nil); m == nil {
t.Error("长度校验失败")
}
}
func Test_Between(t *testing.T) {
rule := "between:6.01, 10.01"
if m := gvalid.Check(context.TODO(), 10, rule, nil); m != nil {
if m := gvalid.CheckValue(context.TODO(), 10, rule, nil); m != nil {
t.Error(m)
}
if m := gvalid.Check(context.TODO(), 10.02, rule, nil); m == nil {
if m := gvalid.CheckValue(context.TODO(), 10.02, rule, nil); m == nil {
t.Error("大小范围校验失败")
}
if m := gvalid.Check(context.TODO(), "a", rule, nil); m == nil {
if m := gvalid.CheckValue(context.TODO(), "a", rule, nil); m == nil {
t.Error("大小范围校验失败")
}
}
@ -723,11 +723,11 @@ func Test_Min(t *testing.T) {
val3 := "100"
val4 := "1000"
val5 := "a"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err5 := gvalid.Check(context.TODO(), val5, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil)
t.AssertNE(err1, nil)
t.AssertNE(err2, nil)
t.Assert(err3, nil)
@ -735,7 +735,7 @@ func Test_Min(t *testing.T) {
t.AssertNE(err5, nil)
rule2 := "min:a"
err6 := gvalid.Check(context.TODO(), val1, rule2, nil)
err6 := gvalid.CheckValue(context.TODO(), val1, rule2, nil)
t.AssertNE(err6, nil)
})
}
@ -748,11 +748,11 @@ func Test_Max(t *testing.T) {
val3 := "100"
val4 := "1000"
val5 := "a"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err5 := gvalid.Check(context.TODO(), val5, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil)
t.Assert(err1, nil)
t.Assert(err2, nil)
t.Assert(err3, nil)
@ -760,7 +760,7 @@ func Test_Max(t *testing.T) {
t.AssertNE(err5, nil)
rule2 := "max:a"
err6 := gvalid.Check(context.TODO(), val1, rule2, nil)
err6 := gvalid.CheckValue(context.TODO(), val1, rule2, nil)
t.AssertNE(err6, nil)
})
}
@ -774,12 +774,12 @@ func Test_Json(t *testing.T) {
val4 := "[]"
val5 := "[1,2,3,4]"
val6 := `{"list":[1,2,3,4]}`
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err5 := gvalid.Check(context.TODO(), val5, rule, nil)
err6 := gvalid.Check(context.TODO(), val6, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil)
err6 := gvalid.CheckValue(context.TODO(), val6, rule, nil)
t.AssertNE(err1, nil)
t.AssertNE(err2, nil)
t.Assert(err3, nil)
@ -798,12 +798,12 @@ func Test_Integer(t *testing.T) {
val4 := "1"
val5 := "100"
val6 := `999999999`
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err5 := gvalid.Check(context.TODO(), val5, rule, nil)
err6 := gvalid.Check(context.TODO(), val6, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil)
err6 := gvalid.CheckValue(context.TODO(), val6, rule, nil)
t.AssertNE(err1, nil)
t.AssertNE(err2, nil)
t.Assert(err3, nil)
@ -822,12 +822,12 @@ func Test_Float(t *testing.T) {
val4 := "1.0"
val5 := "1.1"
val6 := `0.1`
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err5 := gvalid.Check(context.TODO(), val5, rule, nil)
err6 := gvalid.Check(context.TODO(), val6, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil)
err6 := gvalid.CheckValue(context.TODO(), val6, rule, nil)
t.AssertNE(err1, nil)
t.AssertNE(err2, nil)
t.Assert(err3, nil)
@ -846,12 +846,12 @@ func Test_Boolean(t *testing.T) {
val4 := "1"
val5 := "true"
val6 := `off`
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err5 := gvalid.Check(context.TODO(), val5, rule, nil)
err6 := gvalid.Check(context.TODO(), val6, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil)
err6 := gvalid.CheckValue(context.TODO(), val6, rule, nil)
t.AssertNE(err1, nil)
t.AssertNE(err2, nil)
t.Assert(err3, nil)
@ -875,9 +875,9 @@ func Test_Same(t *testing.T) {
"id": 100,
"name": "john",
}
err1 := gvalid.Check(context.TODO(), val1, rule, nil, params1)
err2 := gvalid.Check(context.TODO(), val1, rule, nil, params2)
err3 := gvalid.Check(context.TODO(), val1, rule, nil, params3)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params1)
err2 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params2)
err3 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params3)
t.AssertNE(err1, nil)
t.Assert(err2, nil)
t.Assert(err3, nil)
@ -898,9 +898,9 @@ func Test_Different(t *testing.T) {
"id": 100,
"name": "john",
}
err1 := gvalid.Check(context.TODO(), val1, rule, nil, params1)
err2 := gvalid.Check(context.TODO(), val1, rule, nil, params2)
err3 := gvalid.Check(context.TODO(), val1, rule, nil, params3)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params1)
err2 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params2)
err3 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params3)
t.Assert(err1, nil)
t.AssertNE(err2, nil)
t.AssertNE(err3, nil)
@ -914,10 +914,10 @@ func Test_In(t *testing.T) {
val2 := "1"
val3 := "100"
val4 := "200"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
t.AssertNE(err1, nil)
t.AssertNE(err2, nil)
t.Assert(err3, nil)
@ -932,10 +932,10 @@ func Test_NotIn(t *testing.T) {
val2 := "1"
val3 := "100"
val4 := "200"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
t.Assert(err1, nil)
t.Assert(err2, nil)
t.AssertNE(err3, nil)
@ -947,10 +947,10 @@ func Test_NotIn(t *testing.T) {
val2 := "1"
val3 := "100"
val4 := "200"
err1 := gvalid.Check(context.TODO(), val1, rule, nil)
err2 := gvalid.Check(context.TODO(), val2, rule, nil)
err3 := gvalid.Check(context.TODO(), val3, rule, nil)
err4 := gvalid.Check(context.TODO(), val4, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil)
err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil)
t.Assert(err1, nil)
t.Assert(err2, nil)
t.AssertNE(err3, nil)
@ -960,10 +960,10 @@ func Test_NotIn(t *testing.T) {
func Test_Regex1(t *testing.T) {
rule := `regex:\d{6}|\D{6}|length:6,16`
if m := gvalid.Check(context.TODO(), "123456", rule, nil); m != nil {
if m := gvalid.CheckValue(context.TODO(), "123456", rule, nil); m != nil {
t.Error(m)
}
if m := gvalid.Check(context.TODO(), "abcde6", rule, nil); m == nil {
if m := gvalid.CheckValue(context.TODO(), "abcde6", rule, nil); m == nil {
t.Error("校验失败")
}
}
@ -974,9 +974,9 @@ func Test_Regex2(t *testing.T) {
str1 := ""
str2 := "data"
str3 := "data:image/jpeg;base64,/9jrbattq22r"
err1 := gvalid.Check(context.TODO(), str1, rule, nil)
err2 := gvalid.Check(context.TODO(), str2, rule, nil)
err3 := gvalid.Check(context.TODO(), str3, rule, nil)
err1 := gvalid.CheckValue(context.TODO(), str1, rule, nil)
err2 := gvalid.CheckValue(context.TODO(), str2, rule, nil)
err3 := gvalid.CheckValue(context.TODO(), str3, rule, nil)
t.AssertNE(err1, nil)
t.AssertNE(err2, nil)
t.Assert(err3, nil)

View File

@ -406,7 +406,7 @@ func Test_CheckStruct_InvalidRule(t *testing.T) {
})
}
func TestValidator_CheckStructWithParamMap(t *testing.T) {
func TestValidator_CheckStructWithData(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type UserApiSearch struct {
Uid int64 `v:"required"`
@ -416,7 +416,7 @@ func TestValidator_CheckStructWithParamMap(t *testing.T) {
Uid: 1,
Nickname: "john",
}
t.Assert(gvalid.CheckStructWithParamMap(context.TODO(), data, g.Map{"uid": 1, "nickname": "john"}, nil), nil)
t.Assert(gvalid.CheckStructWithData(context.TODO(), data, g.Map{"uid": 1, "nickname": "john"}, nil), nil)
})
gtest.C(t, func(t *gtest.T) {
type UserApiSearch struct {
@ -424,7 +424,7 @@ func TestValidator_CheckStructWithParamMap(t *testing.T) {
Nickname string `v:"required-with:uid"`
}
data := UserApiSearch{}
t.AssertNE(gvalid.CheckStructWithParamMap(context.TODO(), data, g.Map{}, nil), nil)
t.AssertNE(gvalid.CheckStructWithData(context.TODO(), data, g.Map{}, nil), nil)
})
gtest.C(t, func(t *gtest.T) {
type UserApiSearch struct {
@ -434,7 +434,7 @@ func TestValidator_CheckStructWithParamMap(t *testing.T) {
data := UserApiSearch{
Uid: 1,
}
t.AssertNE(gvalid.CheckStructWithParamMap(context.TODO(), data, g.Map{}, nil), nil)
t.AssertNE(gvalid.CheckStructWithData(context.TODO(), data, g.Map{}, nil), nil)
})
gtest.C(t, func(t *gtest.T) {
@ -448,7 +448,7 @@ func TestValidator_CheckStructWithParamMap(t *testing.T) {
StartTime: nil,
EndTime: nil,
}
t.Assert(gvalid.CheckStructWithParamMap(context.TODO(), data, g.Map{}, nil), nil)
t.Assert(gvalid.CheckStructWithData(context.TODO(), data, g.Map{}, nil), nil)
})
gtest.C(t, func(t *gtest.T) {
type UserApiSearch struct {
@ -461,6 +461,6 @@ func TestValidator_CheckStructWithParamMap(t *testing.T) {
StartTime: gtime.Now(),
EndTime: nil,
}
t.AssertNE(gvalid.CheckStructWithParamMap(context.TODO(), data, g.Map{"start_time": gtime.Now()}, nil), nil)
t.AssertNE(gvalid.CheckStructWithData(context.TODO(), data, g.Map{"start_time": gtime.Now()}, nil), nil)
})
}

View File

@ -31,9 +31,9 @@ func Test_CustomRule1(t *testing.T) {
})
gtest.Assert(err, nil)
gtest.C(t, func(t *gtest.T) {
err := gvalid.Check(context.TODO(), "123456", rule, "custom message")
err := gvalid.CheckValue(context.TODO(), "123456", rule, "custom message")
t.Assert(err.String(), "custom message")
err = gvalid.Check(context.TODO(), "123456", rule, "custom message", g.Map{"data": "123456"})
err = gvalid.CheckValue(context.TODO(), "123456", rule, "custom message", g.Map{"data": "123456"})
t.Assert(err, nil)
})
// Error with struct validation.
@ -77,8 +77,8 @@ func Test_CustomRule2(t *testing.T) {
// Check.
gtest.C(t, func(t *gtest.T) {
errStr := "data map should not be empty"
t.Assert(gvalid.Check(context.TODO(), g.Map{}, rule, errStr).String(), errStr)
t.Assert(gvalid.Check(context.TODO(), g.Map{"k": "v"}, rule, errStr), nil)
t.Assert(gvalid.CheckValue(context.TODO(), g.Map{}, rule, errStr).String(), errStr)
t.Assert(gvalid.CheckValue(context.TODO(), g.Map{"k": "v"}, rule, errStr), nil)
})
// Error with struct validation.
gtest.C(t, func(t *gtest.T) {
@ -121,9 +121,9 @@ func Test_CustomRule_AllowEmpty(t *testing.T) {
// Check.
gtest.C(t, func(t *gtest.T) {
errStr := "error"
t.Assert(gvalid.Check(context.TODO(), "", rule, errStr), nil)
t.Assert(gvalid.Check(context.TODO(), "gf", rule, errStr), nil)
t.Assert(gvalid.Check(context.TODO(), "gf2", rule, errStr).String(), errStr)
t.Assert(gvalid.CheckValue(context.TODO(), "", rule, errStr), nil)
t.Assert(gvalid.CheckValue(context.TODO(), "gf", rule, errStr), nil)
t.Assert(gvalid.CheckValue(context.TODO(), "gf2", rule, errStr).String(), errStr)
})
// Error with struct validation.
gtest.C(t, func(t *gtest.T) {

View File

@ -20,7 +20,7 @@ func Test_Map(t *testing.T) {
var (
rule = "ipv4"
val = "0.0.0"
err = gvalid.Check(context.TODO(), val, rule, nil)
err = gvalid.CheckValue(context.TODO(), val, rule, nil)
msg = map[string]string{
"ipv4": "The value must be a valid IPv4 address",
}
@ -34,7 +34,7 @@ func Test_FirstString(t *testing.T) {
var (
rule = "ipv4"
val = "0.0.0"
err = gvalid.Check(context.TODO(), val, rule, nil)
err = gvalid.CheckValue(context.TODO(), val, rule, nil)
n = err.FirstString()
)
t.Assert(n, "The value must be a valid IPv4 address")
@ -47,7 +47,7 @@ func Test_CustomError1(t *testing.T) {
"integer": "请输入一个整数",
"length": "参数长度不对啊老铁",
}
e := gvalid.Check(context.TODO(), "6.66", rule, msgs)
e := gvalid.CheckValue(context.TODO(), "6.66", rule, msgs)
if e == nil || len(e.Map()) != 2 {
t.Error("规则校验失败")
} else {
@ -67,7 +67,7 @@ func Test_CustomError1(t *testing.T) {
func Test_CustomError2(t *testing.T) {
rule := "integer|length:6,16"
msgs := "请输入一个整数|参数长度不对啊老铁"
e := gvalid.Check(context.TODO(), "6.66", rule, msgs)
e := gvalid.CheckValue(context.TODO(), "6.66", rule, msgs)
if e == nil || len(e.Map()) != 2 {
t.Error("规则校验失败")
} else {

View File

@ -24,14 +24,14 @@ func TestValidator_I18n(t *testing.T) {
validator = gvalid.New().I18n(i18nManager)
)
gtest.C(t, func(t *gtest.T) {
err = validator.Check("", "required", nil)
err = validator.Rules("required").CheckValue("")
t.Assert(err.String(), "The field is required")
err = validator.Ctx(ctxCn).Check("", "required", nil)
err = validator.Ctx(ctxCn).Rules("required").CheckValue("")
t.Assert(err.String(), "字段不能为空")
})
gtest.C(t, func(t *gtest.T) {
err = validator.Ctx(ctxCn).Check("", "required", "CustomMessage")
err = validator.Ctx(ctxCn).Rules("required").Messages("CustomMessage").CheckValue("")
t.Assert(err.String(), "自定义错误")
})
gtest.C(t, func(t *gtest.T) {
@ -44,7 +44,7 @@ func TestValidator_I18n(t *testing.T) {
Page: 1,
Size: 10,
}
err := validator.Ctx(ctxCn).CheckStruct(obj, nil)
err := validator.Ctx(ctxCn).CheckStruct(obj)
t.Assert(err.String(), "项目ID必须大于等于1并且要小于等于10000")
})
}