2021-01-12 10:46:39 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-07-04 11:19:59 +08:00
|
|
|
//
|
|
|
|
// 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_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2021-03-30 16:06:20 +08:00
|
|
|
"time"
|
2019-07-04 11:19:59 +08:00
|
|
|
|
2021-11-13 23:30:31 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
2021-11-13 23:30:31 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gctx"
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2019-07-04 11:19:59 +08:00
|
|
|
)
|
|
|
|
|
2021-10-11 21:34:19 +08:00
|
|
|
var (
|
|
|
|
ctx = gctx.New()
|
|
|
|
)
|
|
|
|
|
2019-07-04 11:19:59 +08:00
|
|
|
func Test_Check(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "abc:6,16"
|
|
|
|
val1 := 0
|
|
|
|
val2 := 7
|
|
|
|
val3 := 20
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
2021-05-19 13:29:40 +08:00
|
|
|
t.Assert(err1, "InvalidRules: abc:6,16")
|
|
|
|
t.Assert(err2, "InvalidRules: abc:6,16")
|
|
|
|
t.Assert(err3, "InvalidRules: abc:6,16")
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Required(t *testing.T) {
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("1").Rules("required").Messages(nil).Run(ctx); m != nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error(m)
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("").Rules("required").Messages(nil).Run(ctx); m == nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error(m)
|
|
|
|
}
|
2021-11-23 10:42:43 +08:00
|
|
|
if m := g.Validator().Data("").Assoc(map[string]interface{}{"id": 1, "age": 19}).Rules("required-if: id,1,age,18").Messages(nil).Run(ctx); m == nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error("Required校验失败")
|
|
|
|
}
|
2021-11-23 10:42:43 +08:00
|
|
|
if m := g.Validator().Data("").Assoc(map[string]interface{}{"id": 2, "age": 19}).Rules("required-if: id,1,age,18").Messages(nil).Run(ctx); m != nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error("Required校验失败")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_RequiredIf(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-05-10 16:48:00 +08:00
|
|
|
rule := "required-if:id,1,age,18"
|
2021-11-23 10:42:43 +08:00
|
|
|
t.AssertNE(g.Validator().Data("").Assoc(g.Map{"id": 1}).Rules(rule).Messages(nil).Run(ctx), nil)
|
|
|
|
t.Assert(g.Validator().Data("").Assoc(g.Map{"id": 0}).Rules(rule).Messages(nil).Run(ctx), nil)
|
|
|
|
t.AssertNE(g.Validator().Data("").Assoc(g.Map{"age": 18}).Rules(rule).Messages(nil).Run(ctx), nil)
|
|
|
|
t.Assert(g.Validator().Data("").Assoc(g.Map{"age": 20}).Rules(rule).Messages(nil).Run(ctx), nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_RequiredUnless(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-05-10 16:48:00 +08:00
|
|
|
rule := "required-unless:id,1,age,18"
|
2021-11-23 10:42:43 +08:00
|
|
|
t.Assert(g.Validator().Data("").Assoc(g.Map{"id": 1}).Rules(rule).Messages(nil).Run(ctx), nil)
|
|
|
|
t.AssertNE(g.Validator().Data("").Assoc(g.Map{"id": 0}).Rules(rule).Messages(nil).Run(ctx), nil)
|
|
|
|
t.Assert(g.Validator().Data("").Assoc(g.Map{"age": 18}).Rules(rule).Messages(nil).Run(ctx), nil)
|
|
|
|
t.AssertNE(g.Validator().Data("").Assoc(g.Map{"age": 20}).Rules(rule).Messages(nil).Run(ctx), nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_RequiredWith(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "required-with:id,name"
|
|
|
|
val1 := ""
|
|
|
|
params1 := g.Map{
|
|
|
|
"age": 18,
|
|
|
|
}
|
|
|
|
params2 := g.Map{
|
|
|
|
"id": 100,
|
|
|
|
}
|
|
|
|
params3 := g.Map{
|
|
|
|
"id": 100,
|
|
|
|
"name": "john",
|
|
|
|
}
|
2021-11-23 10:42:43 +08:00
|
|
|
err1 := g.Validator().Data(val1).Assoc(params1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val1).Assoc(params2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val1).Assoc(params3).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.AssertNE(err3, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
2021-04-26 20:37:36 +08:00
|
|
|
// time.Time
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
rule := "required-with:id,time"
|
|
|
|
val1 := ""
|
|
|
|
params1 := g.Map{
|
|
|
|
"age": 18,
|
|
|
|
}
|
|
|
|
params2 := g.Map{
|
|
|
|
"id": 100,
|
|
|
|
}
|
|
|
|
params3 := g.Map{
|
|
|
|
"time": time.Time{},
|
|
|
|
}
|
2021-11-23 10:42:43 +08:00
|
|
|
err1 := g.Validator().Data(val1).Assoc(params1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val1).Assoc(params2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val1).Assoc(params3).Rules(rule).Messages(nil).Run(ctx)
|
2021-04-26 20:37:36 +08:00
|
|
|
t.Assert(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
rule := "required-with:id,time"
|
|
|
|
val1 := ""
|
|
|
|
params1 := g.Map{
|
|
|
|
"age": 18,
|
|
|
|
}
|
|
|
|
params2 := g.Map{
|
|
|
|
"id": 100,
|
|
|
|
}
|
|
|
|
params3 := g.Map{
|
|
|
|
"time": time.Now(),
|
|
|
|
}
|
2021-11-23 10:42:43 +08:00
|
|
|
err1 := g.Validator().Data(val1).Assoc(params1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val1).Assoc(params2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val1).Assoc(params3).Rules(rule).Messages(nil).Run(ctx)
|
2021-04-26 20:37:36 +08:00
|
|
|
t.Assert(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.AssertNE(err3, nil)
|
|
|
|
})
|
|
|
|
// gtime.Time
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type UserApiSearch struct {
|
|
|
|
Uid int64 `json:"uid"`
|
|
|
|
Nickname string `json:"nickname" v:"required-with:Uid"`
|
|
|
|
StartTime *gtime.Time `json:"start_time" v:"required-with:EndTime"`
|
|
|
|
EndTime *gtime.Time `json:"end_time" v:"required-with:StartTime"`
|
|
|
|
}
|
|
|
|
data := UserApiSearch{
|
|
|
|
StartTime: nil,
|
|
|
|
EndTime: nil,
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
t.Assert(g.Validator().Data(data).Run(ctx), nil)
|
2021-04-26 20:37:36 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type UserApiSearch struct {
|
|
|
|
Uid int64 `json:"uid"`
|
|
|
|
Nickname string `json:"nickname" v:"required-with:Uid"`
|
|
|
|
StartTime *gtime.Time `json:"start_time" v:"required-with:EndTime"`
|
|
|
|
EndTime *gtime.Time `json:"end_time" v:"required-with:StartTime"`
|
|
|
|
}
|
|
|
|
data := UserApiSearch{
|
|
|
|
StartTime: nil,
|
|
|
|
EndTime: gtime.Now(),
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
t.AssertNE(g.Validator().Data(data).Run(ctx), nil)
|
2021-04-26 20:37:36 +08:00
|
|
|
})
|
2019-07-04 11:19:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_RequiredWithAll(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "required-with-all:id,name"
|
|
|
|
val1 := ""
|
|
|
|
params1 := g.Map{
|
|
|
|
"age": 18,
|
|
|
|
}
|
|
|
|
params2 := g.Map{
|
|
|
|
"id": 100,
|
|
|
|
}
|
|
|
|
params3 := g.Map{
|
|
|
|
"id": 100,
|
|
|
|
"name": "john",
|
|
|
|
}
|
2021-11-23 10:42:43 +08:00
|
|
|
err1 := g.Validator().Data(val1).Assoc(params1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val1).Assoc(params2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val1).Assoc(params3).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err1, nil)
|
|
|
|
t.Assert(err2, nil)
|
|
|
|
t.AssertNE(err3, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_RequiredWithOut(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "required-without:id,name"
|
|
|
|
val1 := ""
|
|
|
|
params1 := g.Map{
|
|
|
|
"age": 18,
|
|
|
|
}
|
|
|
|
params2 := g.Map{
|
|
|
|
"id": 100,
|
|
|
|
}
|
|
|
|
params3 := g.Map{
|
|
|
|
"id": 100,
|
|
|
|
"name": "john",
|
|
|
|
}
|
2021-11-23 10:42:43 +08:00
|
|
|
err1 := g.Validator().Data(val1).Assoc(params1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val1).Assoc(params2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val1).Assoc(params3).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_RequiredWithOutAll(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "required-without-all:id,name"
|
|
|
|
val1 := ""
|
|
|
|
params1 := g.Map{
|
|
|
|
"age": 18,
|
|
|
|
}
|
|
|
|
params2 := g.Map{
|
|
|
|
"id": 100,
|
|
|
|
}
|
|
|
|
params3 := g.Map{
|
|
|
|
"id": 100,
|
|
|
|
"name": "john",
|
|
|
|
}
|
2021-11-23 10:42:43 +08:00
|
|
|
err1 := g.Validator().Data(val1).Assoc(params1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val1).Assoc(params2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val1).Assoc(params3).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.Assert(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Date(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "date"
|
|
|
|
val1 := "2010"
|
|
|
|
val2 := "201011"
|
|
|
|
val3 := "20101101"
|
|
|
|
val4 := "2010-11-01"
|
|
|
|
val5 := "2010.11.01"
|
|
|
|
val6 := "2010/11/01"
|
2020-08-24 22:26:12 +08:00
|
|
|
val7 := "2010=11=01"
|
2021-06-01 20:09:52 +08:00
|
|
|
val8 := "123"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data(val5).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err6 := g.Validator().Data(val6).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err7 := g.Validator().Data(val7).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err8 := g.Validator().Data(val8).Rules(rule).Messages(nil).Run(ctx)
|
2021-06-01 20:09:52 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
|
|
|
t.Assert(err5, nil)
|
|
|
|
t.Assert(err6, nil)
|
2020-08-24 22:26:12 +08:00
|
|
|
t.AssertNE(err7, nil)
|
2021-06-01 20:09:52 +08:00
|
|
|
t.AssertNE(err8, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-27 15:33:29 +08:00
|
|
|
func Test_Datetime(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
m := g.MapStrBool{
|
|
|
|
"2010": false,
|
|
|
|
"2010.11": false,
|
|
|
|
"2010-11-01": false,
|
|
|
|
"2010-11-01 12:00": false,
|
|
|
|
"2010-11-01 12:00:00": true,
|
|
|
|
"2010.11.01 12:00:00": false,
|
|
|
|
}
|
|
|
|
for k, v := range m {
|
2021-11-22 22:43:09 +08:00
|
|
|
err := g.Validator().Rules(`datetime`).Data(k).Run(ctx)
|
2021-10-27 15:33:29 +08:00
|
|
|
if v {
|
|
|
|
t.AssertNil(err)
|
|
|
|
} else {
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-04 11:19:59 +08:00
|
|
|
func Test_DateFormat(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
val1 := "2010"
|
|
|
|
val2 := "201011"
|
|
|
|
val3 := "2010.11"
|
|
|
|
val4 := "201011-01"
|
|
|
|
val5 := "2010~11~01"
|
|
|
|
val6 := "2010-11~01"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules("date-format:Y").Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules("date-format:Ym").Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules("date-format:Y.m").Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules("date-format:Ym-d").Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data(val5).Rules("date-format:Y~m~d").Messages(nil).Run(ctx)
|
|
|
|
err6 := g.Validator().Data(val6).Rules("date-format:Y~m~d").Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err1, nil)
|
|
|
|
t.Assert(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
|
|
|
t.Assert(err5, nil)
|
|
|
|
t.AssertNE(err6, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
2021-03-30 16:06:20 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
t1 := gtime.Now()
|
|
|
|
t2 := time.Time{}
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(t1).Rules("date-format:Y").Run(ctx)
|
|
|
|
err2 := g.Validator().Data(t2).Rules("date-format:Y").Run(ctx)
|
2021-03-30 16:06:20 +08:00
|
|
|
t.Assert(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
})
|
2019-07-04 11:19:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Email(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "email"
|
|
|
|
value1 := "m@johngcn"
|
|
|
|
value2 := "m@www@johngcn"
|
|
|
|
value3 := "m-m_m@mail.johng.cn"
|
|
|
|
value4 := "m.m-m@johng.cn"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(value1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(value2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(value3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(value4).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Phone(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data("1361990897").Rules("phone").Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data("13619908979").Rules("phone").Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data("16719908979").Rules("phone").Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data("19719908989").Rules("phone").Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1.String(), nil)
|
|
|
|
t.Assert(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-28 12:23:18 +08:00
|
|
|
func Test_PhoneLoose(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data("13333333333").Rules("phone-loose").Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data("15555555555").Rules("phone-loose").Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data("16666666666").Rules("phone-loose").Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data("23333333333").Rules("phone-loose").Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data("1333333333").Rules("phone-loose").Messages(nil).Run(ctx)
|
|
|
|
err6 := g.Validator().Data("10333333333").Rules("phone-loose").Messages(nil).Run(ctx)
|
2020-11-28 12:23:18 +08:00
|
|
|
t.Assert(err1, nil)
|
|
|
|
t.Assert(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.AssertNE(err4, nil)
|
|
|
|
t.AssertNE(err5, nil)
|
|
|
|
t.AssertNE(err6, nil)
|
|
|
|
})
|
|
|
|
}
|
2021-11-17 23:20:58 +08:00
|
|
|
|
2019-07-04 11:19:59 +08:00
|
|
|
func Test_Telephone(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "telephone"
|
|
|
|
val1 := "869265"
|
|
|
|
val2 := "028-869265"
|
|
|
|
val3 := "86292651"
|
|
|
|
val4 := "028-8692651"
|
|
|
|
val5 := "0830-8692651"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data(val5).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
|
|
|
t.Assert(err5, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Passport(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "passport"
|
|
|
|
val1 := "123456"
|
|
|
|
val2 := "a12345-6"
|
|
|
|
val3 := "aaaaa"
|
|
|
|
val4 := "aaaaaa"
|
|
|
|
val5 := "a123_456"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data(val5).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.AssertNE(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
|
|
|
t.Assert(err5, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Password(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "password"
|
|
|
|
val1 := "12345"
|
|
|
|
val2 := "aaaaa"
|
|
|
|
val3 := "a12345-6"
|
|
|
|
val4 := ">,/;'[09-"
|
|
|
|
val5 := "a123_456"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data(val5).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
|
|
|
t.Assert(err5, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Password2(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "password2"
|
|
|
|
val1 := "12345"
|
|
|
|
val2 := "Naaaa"
|
|
|
|
val3 := "a12345-6"
|
|
|
|
val4 := ">,/;'[09-"
|
|
|
|
val5 := "a123_456"
|
|
|
|
val6 := "Nant1986"
|
|
|
|
val7 := "Nant1986!"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data(val5).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err6 := g.Validator().Data(val6).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err7 := g.Validator().Data(val7).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.AssertNE(err3, nil)
|
|
|
|
t.AssertNE(err4, nil)
|
|
|
|
t.AssertNE(err5, nil)
|
|
|
|
t.Assert(err6, nil)
|
|
|
|
t.Assert(err7, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Password3(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "password3"
|
|
|
|
val1 := "12345"
|
|
|
|
val2 := "Naaaa"
|
|
|
|
val3 := "a12345-6"
|
|
|
|
val4 := ">,/;'[09-"
|
|
|
|
val5 := "a123_456"
|
|
|
|
val6 := "Nant1986"
|
|
|
|
val7 := "Nant1986!"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data(val5).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err6 := g.Validator().Data(val6).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err7 := g.Validator().Data(val7).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.AssertNE(err3, nil)
|
|
|
|
t.AssertNE(err4, nil)
|
|
|
|
t.AssertNE(err5, nil)
|
|
|
|
t.AssertNE(err6, nil)
|
|
|
|
t.Assert(err7, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Postcode(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "postcode"
|
|
|
|
val1 := "12345"
|
|
|
|
val2 := "610036"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.Assert(err2, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-10 16:48:00 +08:00
|
|
|
func Test_ResidentId(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-05-10 16:48:00 +08:00
|
|
|
rule := "resident-id"
|
2019-07-04 11:19:59 +08:00
|
|
|
val1 := "11111111111111"
|
|
|
|
val2 := "1111111111111111"
|
|
|
|
val3 := "311128500121201"
|
|
|
|
val4 := "510521198607185367"
|
|
|
|
val5 := "51052119860718536x"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data(val5).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.AssertNE(err3, nil)
|
|
|
|
t.AssertNE(err4, nil)
|
|
|
|
t.Assert(err5, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-10 16:48:00 +08:00
|
|
|
func Test_BankCard(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-05-10 16:48:00 +08:00
|
|
|
rule := "bank-card"
|
2019-09-24 10:21:37 +08:00
|
|
|
val1 := "6230514630000424470"
|
|
|
|
val2 := "6230514630000424473"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.Assert(err2, nil)
|
2019-09-24 10:21:37 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-04 11:19:59 +08:00
|
|
|
func Test_QQ(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "qq"
|
|
|
|
val1 := "100"
|
|
|
|
val2 := "1"
|
|
|
|
val3 := "10000"
|
|
|
|
val4 := "38996181"
|
|
|
|
val5 := "389961817"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data(val5).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
|
|
|
t.Assert(err5, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Ip(t *testing.T) {
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("10.0.0.1").Rules("ip").Messages(nil).Run(ctx); m != nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error(m)
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("10.0.0.1").Rules("ipv4").Messages(nil).Run(ctx); m != nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error(m)
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("0.0.0.0").Rules("ipv4").Messages(nil).Run(ctx); m != nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error(m)
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("1920.0.0.0").Rules("ipv4").Messages(nil).Run(ctx); m == nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error("ipv4校验失败")
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("1920.0.0.0").Rules("ip").Messages(nil).Run(ctx); m == nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error("ipv4校验失败")
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("fe80::5484:7aff:fefe:9799").Rules("ipv6").Messages(nil).Run(ctx); m != nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error(m)
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("fe80::5484:7aff:fefe:9799123").Rules("ipv6").Messages(nil).Run(ctx); m == nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error(m)
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("fe80::5484:7aff:fefe:9799").Rules("ip").Messages(nil).Run(ctx); m != nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error(m)
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("fe80::5484:7aff:fefe:9799123").Rules("ip").Messages(nil).Run(ctx); m == nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_IPv4(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "ipv4"
|
|
|
|
val1 := "0.0.0"
|
|
|
|
val2 := "0.0.0.0"
|
|
|
|
val3 := "1.1.1.1"
|
|
|
|
val4 := "255.255.255.0"
|
|
|
|
val5 := "127.0.0.1"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data(val5).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.Assert(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
|
|
|
t.Assert(err5, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_IPv6(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "ipv6"
|
|
|
|
val1 := "192.168.1.1"
|
|
|
|
val2 := "CDCD:910A:2222:5498:8475:1111:3900:2020"
|
|
|
|
val3 := "1030::C9B4:FF12:48AA:1A2B"
|
|
|
|
val4 := "2000:0:0:0:0:0:0:1"
|
|
|
|
val5 := "0000:0000:0000:0000:0000:ffff:c0a8:5909"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data(val5).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.Assert(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
|
|
|
t.Assert(err5, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_MAC(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "mac"
|
|
|
|
val1 := "192.168.1.1"
|
|
|
|
val2 := "44-45-53-54-00-00"
|
|
|
|
val3 := "01:00:5e:00:00:00"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.Assert(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_URL(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "url"
|
|
|
|
val1 := "127.0.0.1"
|
|
|
|
val2 := "https://www.baidu.com"
|
|
|
|
val3 := "http://127.0.0.1"
|
|
|
|
val4 := "file:///tmp/test.txt"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.Assert(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Domain(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-05-18 19:44:15 +08:00
|
|
|
m := g.MapStrBool{
|
|
|
|
"localhost": false,
|
|
|
|
"baidu.com": true,
|
|
|
|
"www.baidu.com": true,
|
|
|
|
"jn.np": true,
|
|
|
|
"www.jn.np": true,
|
|
|
|
"w.www.jn.np": true,
|
|
|
|
"127.0.0.1": false,
|
|
|
|
"www.360.com": true,
|
|
|
|
"www.360": false,
|
|
|
|
"360": false,
|
|
|
|
"my-gf": false,
|
|
|
|
"my-gf.com": true,
|
|
|
|
"my-gf.360.com": true,
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
for k, v := range m {
|
2021-11-22 22:43:09 +08:00
|
|
|
err = g.Validator().Data(k).Rules("domain").Messages(nil).Run(ctx)
|
2020-05-18 19:44:15 +08:00
|
|
|
if v {
|
2021-11-13 23:30:31 +08:00
|
|
|
// fmt.Println(k)
|
2020-05-18 19:44:15 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
} else {
|
2021-11-13 23:30:31 +08:00
|
|
|
// fmt.Println(k)
|
2020-05-18 19:44:15 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
}
|
|
|
|
}
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Length(t *testing.T) {
|
|
|
|
rule := "length:6,16"
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("123456").Rules(rule).Messages(nil).Run(ctx); m != nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error(m)
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("12345").Rules(rule).Messages(nil).Run(ctx); m == nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error("长度校验失败")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_MinLength(t *testing.T) {
|
|
|
|
rule := "min-length:6"
|
|
|
|
msgs := map[string]string{
|
2021-11-14 21:00:34 +08:00
|
|
|
"min-length": "地址长度至少为{min}位",
|
2019-07-04 11:19:59 +08:00
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("123456").Rules(rule).Messages(nil).Run(ctx); m != nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error(m)
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("12345").Rules(rule).Messages(nil).Run(ctx); m == nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error("长度校验失败")
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("12345").Rules(rule).Messages(msgs).Run(ctx); m == nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error("长度校验失败")
|
|
|
|
}
|
|
|
|
|
|
|
|
rule2 := "min-length:abc"
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("123456").Rules(rule2).Messages(nil).Run(ctx); m == nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error("长度校验失败")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_MaxLength(t *testing.T) {
|
|
|
|
rule := "max-length:6"
|
|
|
|
msgs := map[string]string{
|
2021-11-14 21:00:34 +08:00
|
|
|
"max-length": "地址长度至大为{max}位",
|
2019-07-04 11:19:59 +08:00
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("12345").Rules(rule).Messages(nil).Run(ctx); m != nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error(m)
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("1234567").Rules(rule).Messages(nil).Run(ctx); m == nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error("长度校验失败")
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("1234567").Rules(rule).Messages(msgs).Run(ctx); m == nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error("长度校验失败")
|
|
|
|
}
|
|
|
|
|
|
|
|
rule2 := "max-length:abc"
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("123456").Rules(rule2).Messages(nil).Run(ctx); m == nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error("长度校验失败")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-29 12:15:37 +08:00
|
|
|
func Test_Size(t *testing.T) {
|
|
|
|
rule := "size:5"
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("12345").Rules(rule).Messages(nil).Run(ctx); m != nil {
|
2021-05-29 12:15:37 +08:00
|
|
|
t.Error(m)
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("123456").Rules(rule).Messages(nil).Run(ctx); m == nil {
|
2021-05-29 12:15:37 +08:00
|
|
|
t.Error("长度校验失败")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-04 11:19:59 +08:00
|
|
|
func Test_Between(t *testing.T) {
|
|
|
|
rule := "between:6.01, 10.01"
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data(10).Rules(rule).Messages(nil).Run(ctx); m != nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error(m)
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data(10.02).Rules(rule).Messages(nil).Run(ctx); m == nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error("大小范围校验失败")
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("a").Rules(rule).Messages(nil).Run(ctx); m == nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error("大小范围校验失败")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Min(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "min:100"
|
|
|
|
val1 := "1"
|
|
|
|
val2 := "99"
|
|
|
|
val3 := "100"
|
|
|
|
val4 := "1000"
|
|
|
|
val5 := "a"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data(val5).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
|
|
|
t.AssertNE(err5, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
|
|
|
|
rule2 := "min:a"
|
2021-11-22 22:43:09 +08:00
|
|
|
err6 := g.Validator().Data(val1).Rules(rule2).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err6, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Max(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "max:100"
|
|
|
|
val1 := "1"
|
|
|
|
val2 := "99"
|
|
|
|
val3 := "100"
|
|
|
|
val4 := "1000"
|
|
|
|
val5 := "a"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data(val5).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err1, nil)
|
|
|
|
t.Assert(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.AssertNE(err4, nil)
|
|
|
|
t.AssertNE(err5, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
|
|
|
|
rule2 := "max:a"
|
2021-11-22 22:43:09 +08:00
|
|
|
err6 := g.Validator().Data(val1).Rules(rule2).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err6, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Json(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "json"
|
|
|
|
val1 := ""
|
|
|
|
val2 := "."
|
|
|
|
val3 := "{}"
|
|
|
|
val4 := "[]"
|
|
|
|
val5 := "[1,2,3,4]"
|
|
|
|
val6 := `{"list":[1,2,3,4]}`
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data(val5).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err6 := g.Validator().Data(val6).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
|
|
|
t.Assert(err5, nil)
|
|
|
|
t.Assert(err6, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Integer(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "integer"
|
|
|
|
val1 := ""
|
|
|
|
val2 := "1.0"
|
|
|
|
val3 := "001"
|
|
|
|
val4 := "1"
|
|
|
|
val5 := "100"
|
|
|
|
val6 := `999999999`
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data(val5).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err6 := g.Validator().Data(val6).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
|
|
|
t.Assert(err5, nil)
|
|
|
|
t.Assert(err6, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Float(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "float"
|
|
|
|
val1 := ""
|
|
|
|
val2 := "a"
|
|
|
|
val3 := "1"
|
|
|
|
val4 := "1.0"
|
|
|
|
val5 := "1.1"
|
|
|
|
val6 := `0.1`
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data(val5).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err6 := g.Validator().Data(val6).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
|
|
|
t.Assert(err5, nil)
|
|
|
|
t.Assert(err6, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Boolean(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "boolean"
|
|
|
|
val1 := "a"
|
|
|
|
val2 := "-"
|
|
|
|
val3 := ""
|
|
|
|
val4 := "1"
|
|
|
|
val5 := "true"
|
|
|
|
val6 := `off`
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err5 := g.Validator().Data(val5).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err6 := g.Validator().Data(val6).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
|
|
|
t.Assert(err5, nil)
|
|
|
|
t.Assert(err6, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Same(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "same:id"
|
|
|
|
val1 := "100"
|
|
|
|
params1 := g.Map{
|
|
|
|
"age": 18,
|
|
|
|
}
|
|
|
|
params2 := g.Map{
|
|
|
|
"id": 100,
|
|
|
|
}
|
|
|
|
params3 := g.Map{
|
|
|
|
"id": 100,
|
|
|
|
"name": "john",
|
|
|
|
}
|
2021-11-23 10:42:43 +08:00
|
|
|
err1 := g.Validator().Data(val1).Assoc(params1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val1).Assoc(params2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val1).Assoc(params3).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.Assert(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Different(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "different:id"
|
|
|
|
val1 := "100"
|
|
|
|
params1 := g.Map{
|
|
|
|
"age": 18,
|
|
|
|
}
|
|
|
|
params2 := g.Map{
|
|
|
|
"id": 100,
|
|
|
|
}
|
|
|
|
params3 := g.Map{
|
|
|
|
"id": 100,
|
|
|
|
"name": "john",
|
|
|
|
}
|
2021-11-23 10:42:43 +08:00
|
|
|
err1 := g.Validator().Data(val1).Assoc(params1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val1).Assoc(params2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val1).Assoc(params3).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.AssertNE(err3, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_In(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "in:100,200"
|
|
|
|
val1 := ""
|
|
|
|
val2 := "1"
|
|
|
|
val3 := "100"
|
|
|
|
val4 := "200"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_NotIn(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-02-10 20:37:53 +08:00
|
|
|
rule := "not-in:100"
|
|
|
|
val1 := ""
|
|
|
|
val2 := "1"
|
|
|
|
val3 := "100"
|
|
|
|
val4 := "200"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err1, nil)
|
|
|
|
t.Assert(err2, nil)
|
|
|
|
t.AssertNE(err3, nil)
|
|
|
|
t.Assert(err4, nil)
|
2020-02-10 20:37:53 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := "not-in:100,200"
|
|
|
|
val1 := ""
|
|
|
|
val2 := "1"
|
|
|
|
val3 := "100"
|
|
|
|
val4 := "200"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(val1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(val2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(val3).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err4 := g.Validator().Data(val4).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err1, nil)
|
|
|
|
t.Assert(err2, nil)
|
|
|
|
t.AssertNE(err3, nil)
|
|
|
|
t.AssertNE(err4, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Regex1(t *testing.T) {
|
|
|
|
rule := `regex:\d{6}|\D{6}|length:6,16`
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("123456").Rules(rule).Messages(nil).Run(ctx); m != nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error(m)
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
if m := g.Validator().Data("abcde6").Rules(rule).Messages(nil).Run(ctx); m == nil {
|
2019-07-04 11:19:59 +08:00
|
|
|
t.Error("校验失败")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Regex2(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-04 11:19:59 +08:00
|
|
|
rule := `required|min-length:6|regex:^data:image\/(jpeg|png);base64,`
|
|
|
|
str1 := ""
|
|
|
|
str2 := "data"
|
|
|
|
str3 := "data:image/jpeg;base64,/9jrbattq22r"
|
2021-11-22 22:43:09 +08:00
|
|
|
err1 := g.Validator().Data(str1).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err2 := g.Validator().Data(str2).Rules(rule).Messages(nil).Run(ctx)
|
|
|
|
err3 := g.Validator().Data(str3).Rules(rule).Messages(nil).Run(ctx)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1, nil)
|
|
|
|
t.AssertNE(err2, nil)
|
|
|
|
t.Assert(err3, nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err1.Map()["required"], nil)
|
|
|
|
t.AssertNE(err2.Map()["min-length"], nil)
|
2019-07-04 11:19:59 +08:00
|
|
|
})
|
|
|
|
}
|
2020-12-23 21:13:47 +08:00
|
|
|
|
|
|
|
// issue: https://github.com/gogf/gf/issues/1077
|
|
|
|
func Test_InternalError_String(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type a struct {
|
|
|
|
Name string `v:"hh"`
|
|
|
|
}
|
|
|
|
aa := a{Name: "2"}
|
2021-11-22 22:43:09 +08:00
|
|
|
err := g.Validator().Data(&aa).Rules(nil).Run(ctx)
|
2020-12-23 21:13:47 +08:00
|
|
|
|
2021-05-19 13:29:40 +08:00
|
|
|
t.Assert(err.String(), "InvalidRules: hh")
|
|
|
|
t.Assert(err.Strings(), g.Slice{"InvalidRules: hh"})
|
2021-11-09 17:50:14 +08:00
|
|
|
t.Assert(err.FirstError(), "InvalidRules: hh")
|
2021-05-19 13:29:40 +08:00
|
|
|
t.Assert(gerror.Current(err), "InvalidRules: hh")
|
2020-12-23 21:13:47 +08:00
|
|
|
})
|
|
|
|
}
|
2021-07-20 23:02:02 +08:00
|
|
|
|
|
|
|
func Test_Code(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-11-22 22:43:09 +08:00
|
|
|
err := g.Validator().Rules("required").Data("").Run(ctx)
|
2021-07-20 23:02:02 +08:00
|
|
|
t.AssertNE(err, nil)
|
2021-08-24 21:18:59 +08:00
|
|
|
t.Assert(gerror.Code(err), gcode.CodeValidationFailed)
|
2021-07-20 23:02:02 +08:00
|
|
|
})
|
2021-08-24 21:31:15 +08:00
|
|
|
|
2021-07-20 23:02:02 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-11-22 22:43:09 +08:00
|
|
|
err := g.Validator().Rules("none-exist-rule").Data("").Run(ctx)
|
2021-07-20 23:02:02 +08:00
|
|
|
t.AssertNE(err, nil)
|
2021-08-24 21:18:59 +08:00
|
|
|
t.Assert(gerror.Code(err), gcode.CodeInternalError)
|
2021-07-20 23:02:02 +08:00
|
|
|
})
|
|
|
|
}
|
2021-08-01 22:12:44 +08:00
|
|
|
|
|
|
|
func Test_Bail(t *testing.T) {
|
|
|
|
// check value with no bail
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := g.Validator().
|
|
|
|
Rules("required|min:1|between:1,100").
|
|
|
|
Messages("|min number is 1|size is between 1 and 100").
|
2021-11-22 22:43:09 +08:00
|
|
|
Data(-1).Run(ctx)
|
2021-08-01 22:12:44 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "min number is 1; size is between 1 and 100")
|
|
|
|
})
|
|
|
|
|
|
|
|
// check value with bail
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := g.Validator().
|
|
|
|
Rules("bail|required|min:1|between:1,100").
|
|
|
|
Messages("||min number is 1|size is between 1 and 100").
|
2021-11-22 22:43:09 +08:00
|
|
|
Data(-1).Run(ctx)
|
2021-08-01 22:12:44 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "min number is 1")
|
|
|
|
})
|
|
|
|
|
|
|
|
// struct with no bail
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type Params struct {
|
|
|
|
Page int `v:"required|min:1"`
|
|
|
|
Size int `v:"required|min:1|between:1,100 # |min number is 1|size is between 1 and 100"`
|
|
|
|
}
|
|
|
|
obj := &Params{
|
|
|
|
Page: 1,
|
|
|
|
Size: -1,
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
err := g.Validator().Data(obj).Run(ctx)
|
2021-08-01 22:12:44 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "min number is 1; size is between 1 and 100")
|
|
|
|
})
|
|
|
|
// struct with bail
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type Params struct {
|
|
|
|
Page int `v:"required|min:1"`
|
|
|
|
Size int `v:"bail|required|min:1|between:1,100 # ||min number is 1|size is between 1 and 100"`
|
|
|
|
}
|
|
|
|
obj := &Params{
|
|
|
|
Page: 1,
|
|
|
|
Size: -1,
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
err := g.Validator().Data(obj).Run(ctx)
|
2021-08-01 22:12:44 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "min number is 1")
|
|
|
|
})
|
|
|
|
}
|