2021-01-12 10:46:39 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-07-05 18:55:38 +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 (
|
2021-05-13 20:56:52 +08:00
|
|
|
"context"
|
2020-07-05 18:55:38 +08:00
|
|
|
"errors"
|
2021-05-19 22:33:50 +08:00
|
|
|
"testing"
|
|
|
|
|
2022-01-14 16:46:25 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2022-01-14 16:46:25 +08:00
|
|
|
"github.com/gogf/gf/v2/util/guid"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/util/gvalid"
|
2020-07-05 18:55:38 +08:00
|
|
|
)
|
|
|
|
|
2020-09-21 23:51:30 +08:00
|
|
|
func Test_CustomRule1(t *testing.T) {
|
2020-07-05 18:55:38 +08:00
|
|
|
rule := "custom"
|
2021-11-09 15:22:17 +08:00
|
|
|
gvalid.RegisterRule(
|
2021-05-29 11:30:34 +08:00
|
|
|
rule,
|
2021-11-09 14:31:16 +08:00
|
|
|
func(ctx context.Context, in gvalid.RuleFuncInput) error {
|
|
|
|
pass := in.Value.String()
|
2021-05-29 11:30:34 +08:00
|
|
|
if len(pass) != 6 {
|
2021-11-09 14:31:16 +08:00
|
|
|
return errors.New(in.Message)
|
2021-05-29 11:30:34 +08:00
|
|
|
}
|
2021-11-09 14:31:16 +08:00
|
|
|
m := in.Data.Map()
|
2021-05-29 11:30:34 +08:00
|
|
|
if m["data"] != pass {
|
2021-11-09 14:31:16 +08:00
|
|
|
return errors.New(in.Message)
|
2021-05-29 11:30:34 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
2021-11-09 15:22:17 +08:00
|
|
|
|
2020-07-05 18:55:38 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-11-22 22:43:09 +08:00
|
|
|
err := g.Validator().Data("123456").Rules(rule).Messages("custom message").Run(ctx)
|
2020-07-05 18:55:38 +08:00
|
|
|
t.Assert(err.String(), "custom message")
|
2021-11-23 10:42:43 +08:00
|
|
|
err = g.Validator().Data("123456").Assoc(g.Map{"data": "123456"}).Rules(rule).Messages("custom message").Run(ctx)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-07-05 18:55:38 +08:00
|
|
|
})
|
|
|
|
// Error with struct validation.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type T struct {
|
|
|
|
Value string `v:"uid@custom#自定义错误"`
|
|
|
|
Data string `p:"data"`
|
|
|
|
}
|
|
|
|
st := &T{
|
|
|
|
Value: "123",
|
|
|
|
Data: "123456",
|
|
|
|
}
|
2021-12-04 00:11:57 +08:00
|
|
|
err := g.Validator().Data(st).Run(ctx)
|
2020-07-05 18:55:38 +08:00
|
|
|
t.Assert(err.String(), "自定义错误")
|
|
|
|
})
|
|
|
|
// No error with struct validation.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type T struct {
|
|
|
|
Value string `v:"uid@custom#自定义错误"`
|
|
|
|
Data string `p:"data"`
|
|
|
|
}
|
|
|
|
st := &T{
|
|
|
|
Value: "123456",
|
|
|
|
Data: "123456",
|
|
|
|
}
|
2021-12-04 00:11:57 +08:00
|
|
|
err := g.Validator().Data(st).Run(ctx)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-07-05 18:55:38 +08:00
|
|
|
})
|
|
|
|
}
|
2020-09-21 23:51:30 +08:00
|
|
|
|
|
|
|
func Test_CustomRule2(t *testing.T) {
|
|
|
|
rule := "required-map"
|
2021-11-09 15:22:17 +08:00
|
|
|
gvalid.RegisterRule(rule, func(ctx context.Context, in gvalid.RuleFuncInput) error {
|
2021-11-09 14:31:16 +08:00
|
|
|
m := in.Value.Map()
|
2020-09-21 23:51:30 +08:00
|
|
|
if len(m) == 0 {
|
2021-11-09 14:31:16 +08:00
|
|
|
return errors.New(in.Message)
|
2020-09-21 23:51:30 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
// Check.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
errStr := "data map should not be empty"
|
2021-11-22 22:43:09 +08:00
|
|
|
t.Assert(g.Validator().Data(g.Map{}).Messages(errStr).Rules(rule).Run(ctx), errStr)
|
|
|
|
t.Assert(g.Validator().Data(g.Map{"k": "v"}).Rules(rule).Messages(errStr).Run(ctx), nil)
|
2020-09-21 23:51:30 +08:00
|
|
|
})
|
|
|
|
// Error with struct validation.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type T struct {
|
|
|
|
Value map[string]string `v:"uid@required-map#自定义错误"`
|
|
|
|
Data string `p:"data"`
|
|
|
|
}
|
|
|
|
st := &T{
|
|
|
|
Value: map[string]string{},
|
|
|
|
Data: "123456",
|
|
|
|
}
|
2021-12-04 00:11:57 +08:00
|
|
|
err := g.Validator().Data(st).Run(ctx)
|
2020-09-21 23:51:30 +08:00
|
|
|
t.Assert(err.String(), "自定义错误")
|
|
|
|
})
|
|
|
|
// No error with struct validation.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type T struct {
|
|
|
|
Value map[string]string `v:"uid@required-map#自定义错误"`
|
|
|
|
Data string `p:"data"`
|
|
|
|
}
|
|
|
|
st := &T{
|
|
|
|
Value: map[string]string{"k": "v"},
|
|
|
|
Data: "123456",
|
|
|
|
}
|
2021-12-04 00:11:57 +08:00
|
|
|
err := g.Validator().Data(st).Run(ctx)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-09-21 23:51:30 +08:00
|
|
|
})
|
|
|
|
}
|
2020-10-21 00:08:36 +08:00
|
|
|
|
|
|
|
func Test_CustomRule_AllowEmpty(t *testing.T) {
|
|
|
|
rule := "allow-empty-str"
|
2021-11-09 15:22:17 +08:00
|
|
|
gvalid.RegisterRule(rule, func(ctx context.Context, in gvalid.RuleFuncInput) error {
|
2021-11-09 14:31:16 +08:00
|
|
|
s := in.Value.String()
|
2020-10-21 00:08:36 +08:00
|
|
|
if len(s) == 0 || s == "gf" {
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-09 14:31:16 +08:00
|
|
|
return errors.New(in.Message)
|
2020-10-21 00:08:36 +08:00
|
|
|
})
|
|
|
|
// Check.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
errStr := "error"
|
2021-11-22 22:43:09 +08:00
|
|
|
t.Assert(g.Validator().Data("").Rules(rule).Messages(errStr).Run(ctx), "")
|
|
|
|
t.Assert(g.Validator().Data("gf").Rules(rule).Messages(errStr).Run(ctx), "")
|
|
|
|
t.Assert(g.Validator().Data("gf2").Rules(rule).Messages(errStr).Run(ctx), errStr)
|
2020-10-21 00:08:36 +08:00
|
|
|
})
|
|
|
|
// Error with struct validation.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type T struct {
|
|
|
|
Value string `v:"uid@allow-empty-str#自定义错误"`
|
|
|
|
Data string `p:"data"`
|
|
|
|
}
|
|
|
|
st := &T{
|
|
|
|
Value: "",
|
|
|
|
Data: "123456",
|
|
|
|
}
|
2021-12-04 00:11:57 +08:00
|
|
|
err := g.Validator().Data(st).Run(ctx)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-10-21 00:08:36 +08:00
|
|
|
})
|
|
|
|
// No error with struct validation.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type T struct {
|
|
|
|
Value string `v:"uid@allow-empty-str#自定义错误"`
|
|
|
|
Data string `p:"data"`
|
|
|
|
}
|
|
|
|
st := &T{
|
|
|
|
Value: "john",
|
|
|
|
Data: "123456",
|
|
|
|
}
|
2021-12-04 00:11:57 +08:00
|
|
|
err := g.Validator().Data(st).Run(ctx)
|
2020-10-21 00:08:36 +08:00
|
|
|
t.Assert(err.String(), "自定义错误")
|
|
|
|
})
|
|
|
|
}
|
2021-05-29 11:30:34 +08:00
|
|
|
|
|
|
|
func TestValidator_RuleFunc(t *testing.T) {
|
|
|
|
ruleName := "custom_1"
|
2021-11-09 14:31:16 +08:00
|
|
|
ruleFunc := func(ctx context.Context, in gvalid.RuleFuncInput) error {
|
|
|
|
pass := in.Value.String()
|
2021-05-29 11:30:34 +08:00
|
|
|
if len(pass) != 6 {
|
2021-11-09 14:31:16 +08:00
|
|
|
return errors.New(in.Message)
|
2021-05-29 11:30:34 +08:00
|
|
|
}
|
2021-11-09 14:31:16 +08:00
|
|
|
if m := in.Data.Map(); m["data"] != pass {
|
|
|
|
return errors.New(in.Message)
|
2021-05-29 11:30:34 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-10-11 21:34:19 +08:00
|
|
|
err := g.Validator().Rules(ruleName).
|
|
|
|
Messages("custom message").
|
|
|
|
RuleFunc(ruleName, ruleFunc).
|
2021-11-22 22:43:09 +08:00
|
|
|
Data("123456").
|
|
|
|
Run(ctx)
|
2021-05-29 11:30:34 +08:00
|
|
|
t.Assert(err.String(), "custom message")
|
|
|
|
err = g.Validator().
|
|
|
|
Rules(ruleName).
|
|
|
|
Messages("custom message").
|
2021-11-23 10:42:43 +08:00
|
|
|
Data("123456").Assoc(g.Map{"data": "123456"}).
|
2021-05-29 11:30:34 +08:00
|
|
|
RuleFunc(ruleName, ruleFunc).
|
2021-11-22 22:43:09 +08:00
|
|
|
Run(ctx)
|
2021-05-29 11:30:34 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
})
|
|
|
|
// Error with struct validation.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type T struct {
|
|
|
|
Value string `v:"uid@custom_1#自定义错误"`
|
|
|
|
Data string `p:"data"`
|
|
|
|
}
|
|
|
|
st := &T{
|
|
|
|
Value: "123",
|
|
|
|
Data: "123456",
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
err := g.Validator().RuleFunc(ruleName, ruleFunc).Data(st).Run(ctx)
|
2021-05-29 11:30:34 +08:00
|
|
|
t.Assert(err.String(), "自定义错误")
|
|
|
|
})
|
|
|
|
// No error with struct validation.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type T struct {
|
|
|
|
Value string `v:"uid@custom_1#自定义错误"`
|
|
|
|
Data string `p:"data"`
|
|
|
|
}
|
|
|
|
st := &T{
|
|
|
|
Value: "123456",
|
|
|
|
Data: "123456",
|
|
|
|
}
|
2021-11-22 22:43:09 +08:00
|
|
|
err := g.Validator().RuleFunc(ruleName, ruleFunc).Data(st).Run(ctx)
|
2021-05-29 11:30:34 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidator_RuleFuncMap(t *testing.T) {
|
|
|
|
ruleName := "custom_1"
|
2021-11-09 14:31:16 +08:00
|
|
|
ruleFunc := func(ctx context.Context, in gvalid.RuleFuncInput) error {
|
|
|
|
pass := in.Value.String()
|
2021-05-29 11:30:34 +08:00
|
|
|
if len(pass) != 6 {
|
2021-11-09 14:31:16 +08:00
|
|
|
return errors.New(in.Message)
|
2021-05-29 11:30:34 +08:00
|
|
|
}
|
2021-11-09 14:31:16 +08:00
|
|
|
if m := in.Data.Map(); m["data"] != pass {
|
|
|
|
return errors.New(in.Message)
|
2021-05-29 11:30:34 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := g.Validator().
|
|
|
|
Rules(ruleName).
|
|
|
|
Messages("custom message").
|
|
|
|
RuleFuncMap(map[string]gvalid.RuleFunc{
|
|
|
|
ruleName: ruleFunc,
|
2021-11-22 22:43:09 +08:00
|
|
|
}).Data("123456").Run(ctx)
|
2021-05-29 11:30:34 +08:00
|
|
|
t.Assert(err.String(), "custom message")
|
|
|
|
err = g.Validator().
|
|
|
|
Rules(ruleName).
|
|
|
|
Messages("custom message").
|
2021-11-23 10:42:43 +08:00
|
|
|
Data("123456").Assoc(g.Map{"data": "123456"}).
|
2021-05-29 11:30:34 +08:00
|
|
|
RuleFuncMap(map[string]gvalid.RuleFunc{
|
|
|
|
ruleName: ruleFunc,
|
2021-11-22 22:43:09 +08:00
|
|
|
}).Run(ctx)
|
2021-05-29 11:30:34 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
})
|
|
|
|
// Error with struct validation.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type T struct {
|
|
|
|
Value string `v:"uid@custom_1#自定义错误"`
|
|
|
|
Data string `p:"data"`
|
|
|
|
}
|
|
|
|
st := &T{
|
|
|
|
Value: "123",
|
|
|
|
Data: "123456",
|
|
|
|
}
|
|
|
|
err := g.Validator().
|
|
|
|
RuleFuncMap(map[string]gvalid.RuleFunc{
|
|
|
|
ruleName: ruleFunc,
|
2021-11-22 22:43:09 +08:00
|
|
|
}).Data(st).Run(ctx)
|
2021-05-29 11:30:34 +08:00
|
|
|
t.Assert(err.String(), "自定义错误")
|
|
|
|
})
|
|
|
|
// No error with struct validation.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type T struct {
|
|
|
|
Value string `v:"uid@custom_1#自定义错误"`
|
|
|
|
Data string `p:"data"`
|
|
|
|
}
|
|
|
|
st := &T{
|
|
|
|
Value: "123456",
|
|
|
|
Data: "123456",
|
|
|
|
}
|
|
|
|
err := g.Validator().
|
|
|
|
RuleFuncMap(map[string]gvalid.RuleFunc{
|
|
|
|
ruleName: ruleFunc,
|
2021-11-22 22:43:09 +08:00
|
|
|
}).Data(st).Run(ctx)
|
2021-05-29 11:30:34 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
})
|
|
|
|
}
|
2022-01-14 16:46:25 +08:00
|
|
|
|
|
|
|
func Test_CustomRule_Overwrite(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var rule = "custom-" + guid.S()
|
|
|
|
gvalid.RegisterRule(rule, func(ctx context.Context, in gvalid.RuleFuncInput) error {
|
|
|
|
return gerror.New("1")
|
|
|
|
})
|
|
|
|
t.Assert(g.Validator().Rules(rule).Data(1).Run(ctx), "1")
|
|
|
|
gvalid.RegisterRule(rule, func(ctx context.Context, in gvalid.RuleFuncInput) error {
|
|
|
|
return gerror.New("2")
|
|
|
|
})
|
|
|
|
t.Assert(g.Validator().Rules(rule).Data(1).Run(ctx), "2")
|
|
|
|
})
|
|
|
|
g.Dump(gvalid.GetRegisteredRuleMap())
|
|
|
|
}
|
2023-03-13 22:16:57 +08:00
|
|
|
|
|
|
|
func Test_Issue2499(t *testing.T) {
|
|
|
|
ruleName := "required"
|
|
|
|
ruleFunc := func(ctx context.Context, in gvalid.RuleFuncInput) error {
|
|
|
|
return errors.New(in.Message)
|
|
|
|
}
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type T struct {
|
|
|
|
Value string `v:"uid@required"`
|
|
|
|
Data string `p:"data"`
|
|
|
|
}
|
|
|
|
st := &T{
|
|
|
|
Value: "",
|
|
|
|
Data: "123456",
|
|
|
|
}
|
|
|
|
err := g.Validator().
|
|
|
|
RuleFuncMap(map[string]gvalid.RuleFunc{
|
|
|
|
ruleName: ruleFunc,
|
|
|
|
}).Data(st).Run(ctx)
|
|
|
|
t.Assert(err.String(), `The uid field is required`)
|
|
|
|
})
|
|
|
|
}
|