mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
move sample function to right file
This commit is contained in:
@ -10,8 +10,10 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/i18n/gi18n"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/gogf/gf/v2/util/gvalid"
|
||||
)
|
||||
@ -187,6 +189,160 @@ func ExampleValidator_Data() {
|
||||
// The Password2 value `gofra` is not a valid password format
|
||||
}
|
||||
|
||||
func ExampleValidator_Data_Map1() {
|
||||
params := map[string]interface{}{
|
||||
"passport": "",
|
||||
"password": "123456",
|
||||
"password2": "1234567",
|
||||
}
|
||||
rules := []string{
|
||||
"passport@required|length:6,16#账号不能为空|账号长度应当在{min}到{max}之间",
|
||||
"password@required|length:6,16|same{password}2#密码不能为空|密码长度应当在{min}到{max}之间|两次密码输入不相等",
|
||||
"password2@required|length:6,16#",
|
||||
}
|
||||
if e := g.Validator().Data(params).Rules(rules).Run(gctx.New()); e != nil {
|
||||
fmt.Println(e.Map())
|
||||
fmt.Println(e.FirstItem())
|
||||
fmt.Println(e.FirstError())
|
||||
}
|
||||
// May Output:
|
||||
// map[required:账号不能为空 length:账号长度应当在6到16之间]
|
||||
// passport map[required:账号不能为空 length:账号长度应当在6到16之间]
|
||||
// 账号不能为空
|
||||
}
|
||||
|
||||
func ExampleValidator_Data_Map2() {
|
||||
params := map[string]interface{}{
|
||||
"passport": "",
|
||||
"password": "123456",
|
||||
"password2": "1234567",
|
||||
}
|
||||
rules := []string{
|
||||
"passport@length:6,16#账号不能为空|账号长度应当在{min}到{max}之间",
|
||||
"password@required|length:6,16|same:password2#密码不能为空|密码长度应当在{min}到{max}之间|两次密码输入不相等",
|
||||
"password2@required|length:6,16#",
|
||||
}
|
||||
if e := g.Validator().Data(params).Rules(rules).Run(gctx.New()); e != nil {
|
||||
fmt.Println(e.Map())
|
||||
fmt.Println(e.FirstItem())
|
||||
fmt.Println(e.FirstError())
|
||||
}
|
||||
// Output:
|
||||
// map[same:两次密码输入不相等]
|
||||
// password map[same:两次密码输入不相等]
|
||||
// 两次密码输入不相等
|
||||
}
|
||||
|
||||
func ExampleValidator_Data_Map3() {
|
||||
params := map[string]interface{}{
|
||||
"passport": "",
|
||||
"password": "123456",
|
||||
"password2": "1234567",
|
||||
}
|
||||
rules := map[string]string{
|
||||
"passport": "required|length:6,16",
|
||||
"password": "required|length:6,16|same:password2",
|
||||
"password2": "required|length:6,16",
|
||||
}
|
||||
messages := map[string]interface{}{
|
||||
"passport": "账号不能为空|账号长度应当在{min}到{max}之间",
|
||||
"password": map[string]string{
|
||||
"required": "密码不能为空",
|
||||
"same": "两次密码输入不相等",
|
||||
},
|
||||
}
|
||||
err := g.Validator().
|
||||
Messages(messages).
|
||||
Rules(rules).
|
||||
Data(params).Run(gctx.New())
|
||||
if err != nil {
|
||||
g.Dump(err.Maps())
|
||||
}
|
||||
|
||||
// May Output:
|
||||
// {
|
||||
// "passport": {
|
||||
// "length": "账号长度应当在6到16之间",
|
||||
// "required": "账号不能为空"
|
||||
// },
|
||||
// "password": {
|
||||
// "same": "两次密码输入不相等"
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
// Empty string attribute.
|
||||
func ExampleValidator_Data_Struct1() {
|
||||
type Params struct {
|
||||
Page int `v:"required|min:1 # page is required"`
|
||||
Size int `v:"required|between:1,100 # size is required"`
|
||||
ProjectId string `v:"between:1,10000 # project id must between {min}, {max}"`
|
||||
}
|
||||
obj := &Params{
|
||||
Page: 1,
|
||||
Size: 10,
|
||||
}
|
||||
err := g.Validator().Data(obj).Run(gctx.New())
|
||||
fmt.Println(err == nil)
|
||||
// Output:
|
||||
// true
|
||||
}
|
||||
|
||||
// Empty pointer attribute.
|
||||
func ExampleValidator_Data_Struct2() {
|
||||
type Params struct {
|
||||
Page int `v:"required|min:1 # page is required"`
|
||||
Size int `v:"required|between:1,100 # size is required"`
|
||||
ProjectId *gvar.Var `v:"between:1,10000 # project id must between {min}, {max}"`
|
||||
}
|
||||
obj := &Params{
|
||||
Page: 1,
|
||||
Size: 10,
|
||||
}
|
||||
err := g.Validator().Data(obj).Run(gctx.New())
|
||||
fmt.Println(err == nil)
|
||||
// Output:
|
||||
// true
|
||||
}
|
||||
|
||||
// Empty integer attribute.
|
||||
func ExampleValidator_Data_Struct3() {
|
||||
type Params struct {
|
||||
Page int `v:"required|min:1 # page is required"`
|
||||
Size int `v:"required|between:1,100 # size is required"`
|
||||
ProjectId int `v:"between:1,10000 # project id must between {min}, {max}"`
|
||||
}
|
||||
obj := &Params{
|
||||
Page: 1,
|
||||
Size: 10,
|
||||
}
|
||||
err := g.Validator().Data(obj).Run(gctx.New())
|
||||
fmt.Println(err)
|
||||
// Output:
|
||||
// project id must between 1, 10000
|
||||
}
|
||||
|
||||
func ExampleValidator_Data_Struct4() {
|
||||
type User struct {
|
||||
Name string `v:"required#请输入用户姓名"`
|
||||
Type int `v:"required#请选择用户类型"`
|
||||
}
|
||||
data := g.Map{
|
||||
"name": "john",
|
||||
}
|
||||
user := User{}
|
||||
if err := gconv.Scan(data, &user); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err := g.Validator().Data(user).Assoc(data).Run(gctx.New())
|
||||
if err != nil {
|
||||
fmt.Println(err.Items())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// [map[Type:map[required:请选择用户类型]]]
|
||||
}
|
||||
|
||||
func ExampleValidator_Assoc() {
|
||||
type User struct {
|
||||
Name string `v:"required"`
|
||||
@ -323,3 +479,46 @@ func ExampleValidator_RuleFuncMap() {
|
||||
// Output:
|
||||
// Value Length Error!; Pass is not Same!
|
||||
}
|
||||
|
||||
func ExampleValidator_Data_Value() {
|
||||
err := g.Validator().Rules("min:18").
|
||||
Messages("未成年人不允许注册哟").
|
||||
Data(16).Run(gctx.New())
|
||||
fmt.Println(err.String())
|
||||
|
||||
// Output:
|
||||
// 未成年人不允许注册哟
|
||||
}
|
||||
|
||||
func ExampleValidator_RegisterRule() {
|
||||
type User struct {
|
||||
Id int
|
||||
Name string `v:"required|unique-name # 请输入用户名称|用户名称已被占用"`
|
||||
Pass string `v:"required|length:6,18"`
|
||||
}
|
||||
user := &User{
|
||||
Id: 1,
|
||||
Name: "john",
|
||||
Pass: "123456",
|
||||
}
|
||||
|
||||
rule := "unique-name"
|
||||
gvalid.RegisterRule(rule, func(ctx context.Context, in gvalid.RuleFuncInput) error {
|
||||
var (
|
||||
id = in.Data.Val().(*User).Id
|
||||
name = gconv.String(in.Value)
|
||||
)
|
||||
n, err := g.Model("user").Where("id != ? and name = ?", id, name).Count()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n > 0 {
|
||||
return errors.New(in.Message)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
err := g.Validator().Data(user).Run(gctx.New())
|
||||
fmt.Println(err.Error())
|
||||
// May Output:
|
||||
// 用户名称已被占用
|
||||
}
|
||||
|
||||
@ -8,17 +8,9 @@ package gvalid_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"math"
|
||||
"reflect"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/gogf/gf/v2/util/gvalid"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
|
||||
func Example_Rule_Required() {
|
||||
@ -1110,246 +1102,3 @@ func Example_Rule_Regex() {
|
||||
// The Regex1 value `1234` must be in regex of: [1-9][0-9]{4,14}
|
||||
// The Regex2 value `01234` must be in regex of: [1-9][0-9]{4,14}
|
||||
}
|
||||
|
||||
func ExampleValidator_Data_Map1() {
|
||||
params := map[string]interface{}{
|
||||
"passport": "",
|
||||
"password": "123456",
|
||||
"password2": "1234567",
|
||||
}
|
||||
rules := []string{
|
||||
"passport@required|length:6,16#账号不能为空|账号长度应当在{min}到{max}之间",
|
||||
"password@required|length:6,16|same{password}2#密码不能为空|密码长度应当在{min}到{max}之间|两次密码输入不相等",
|
||||
"password2@required|length:6,16#",
|
||||
}
|
||||
if e := g.Validator().Data(params).Rules(rules).Run(gctx.New()); e != nil {
|
||||
fmt.Println(e.Map())
|
||||
fmt.Println(e.FirstItem())
|
||||
fmt.Println(e.FirstError())
|
||||
}
|
||||
// May Output:
|
||||
// map[required:账号不能为空 length:账号长度应当在6到16之间]
|
||||
// passport map[required:账号不能为空 length:账号长度应当在6到16之间]
|
||||
// 账号不能为空
|
||||
}
|
||||
|
||||
func ExampleValidator_Data_Map2() {
|
||||
params := map[string]interface{}{
|
||||
"passport": "",
|
||||
"password": "123456",
|
||||
"password2": "1234567",
|
||||
}
|
||||
rules := []string{
|
||||
"passport@length:6,16#账号不能为空|账号长度应当在{min}到{max}之间",
|
||||
"password@required|length:6,16|same:password2#密码不能为空|密码长度应当在{min}到{max}之间|两次密码输入不相等",
|
||||
"password2@required|length:6,16#",
|
||||
}
|
||||
if e := g.Validator().Data(params).Rules(rules).Run(gctx.New()); e != nil {
|
||||
fmt.Println(e.Map())
|
||||
fmt.Println(e.FirstItem())
|
||||
fmt.Println(e.FirstError())
|
||||
}
|
||||
// Output:
|
||||
// map[same:两次密码输入不相等]
|
||||
// password map[same:两次密码输入不相等]
|
||||
// 两次密码输入不相等
|
||||
}
|
||||
|
||||
// Empty string attribute.
|
||||
func ExampleValidator_Data_Struct1() {
|
||||
type Params struct {
|
||||
Page int `v:"required|min:1 # page is required"`
|
||||
Size int `v:"required|between:1,100 # size is required"`
|
||||
ProjectId string `v:"between:1,10000 # project id must between {min}, {max}"`
|
||||
}
|
||||
obj := &Params{
|
||||
Page: 1,
|
||||
Size: 10,
|
||||
}
|
||||
err := g.Validator().Data(obj).Run(gctx.New())
|
||||
fmt.Println(err == nil)
|
||||
// Output:
|
||||
// true
|
||||
}
|
||||
|
||||
// Empty pointer attribute.
|
||||
func ExampleValidator_Data_Struct2() {
|
||||
type Params struct {
|
||||
Page int `v:"required|min:1 # page is required"`
|
||||
Size int `v:"required|between:1,100 # size is required"`
|
||||
ProjectId *gvar.Var `v:"between:1,10000 # project id must between {min}, {max}"`
|
||||
}
|
||||
obj := &Params{
|
||||
Page: 1,
|
||||
Size: 10,
|
||||
}
|
||||
err := g.Validator().Data(obj).Run(gctx.New())
|
||||
fmt.Println(err == nil)
|
||||
// Output:
|
||||
// true
|
||||
}
|
||||
|
||||
// Empty integer attribute.
|
||||
func ExampleValidator_Data_Struct3() {
|
||||
type Params struct {
|
||||
Page int `v:"required|min:1 # page is required"`
|
||||
Size int `v:"required|between:1,100 # size is required"`
|
||||
ProjectId int `v:"between:1,10000 # project id must between {min}, {max}"`
|
||||
}
|
||||
obj := &Params{
|
||||
Page: 1,
|
||||
Size: 10,
|
||||
}
|
||||
err := g.Validator().Data(obj).Run(gctx.New())
|
||||
fmt.Println(err)
|
||||
// Output:
|
||||
// project id must between 1, 10000
|
||||
}
|
||||
|
||||
func ExampleRegisterRule() {
|
||||
type User struct {
|
||||
Id int
|
||||
Name string `v:"required|unique-name # 请输入用户名称|用户名称已被占用"`
|
||||
Pass string `v:"required|length:6,18"`
|
||||
}
|
||||
user := &User{
|
||||
Id: 1,
|
||||
Name: "john",
|
||||
Pass: "123456",
|
||||
}
|
||||
|
||||
rule := "unique-name"
|
||||
gvalid.RegisterRule(rule, func(ctx context.Context, in gvalid.RuleFuncInput) error {
|
||||
var (
|
||||
id = in.Data.Val().(*User).Id
|
||||
name = gconv.String(in.Value)
|
||||
)
|
||||
n, err := g.Model("user").Where("id != ? and name = ?", id, name).Count()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n > 0 {
|
||||
return errors.New(in.Message)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
err := g.Validator().Data(user).Run(gctx.New())
|
||||
fmt.Println(err.Error())
|
||||
// May Output:
|
||||
// 用户名称已被占用
|
||||
}
|
||||
|
||||
func ExampleRegisterRule_OverwriteRequired() {
|
||||
rule := "required"
|
||||
gvalid.RegisterRule(rule, func(ctx context.Context, in gvalid.RuleFuncInput) error {
|
||||
reflectValue := reflect.ValueOf(in.Value.Val())
|
||||
if reflectValue.Kind() == reflect.Ptr {
|
||||
reflectValue = reflectValue.Elem()
|
||||
}
|
||||
isEmpty := false
|
||||
switch reflectValue.Kind() {
|
||||
case reflect.Bool:
|
||||
isEmpty = !reflectValue.Bool()
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
isEmpty = reflectValue.Int() == 0
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
isEmpty = reflectValue.Uint() == 0
|
||||
case reflect.Float32, reflect.Float64:
|
||||
isEmpty = math.Float64bits(reflectValue.Float()) == 0
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
c := reflectValue.Complex()
|
||||
isEmpty = math.Float64bits(real(c)) == 0 && math.Float64bits(imag(c)) == 0
|
||||
case reflect.String, reflect.Map, reflect.Array, reflect.Slice:
|
||||
isEmpty = reflectValue.Len() == 0
|
||||
}
|
||||
if isEmpty {
|
||||
return errors.New(in.Message)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
fmt.Println(g.Validator().Data("").Rules("required").Messages("It's required").Run(gctx.New()))
|
||||
fmt.Println(g.Validator().Data(0).Rules("required").Messages("It's required").Run(gctx.New()))
|
||||
fmt.Println(g.Validator().Data(false).Rules("required").Messages("It's required").Run(gctx.New()))
|
||||
gvalid.DeleteRule(rule)
|
||||
fmt.Println("rule deleted")
|
||||
fmt.Println(g.Validator().Data("").Rules("required").Messages("It's required").Run(gctx.New()))
|
||||
fmt.Println(g.Validator().Data(0).Rules("required").Messages("It's required").Run(gctx.New()))
|
||||
fmt.Println(g.Validator().Data(false).Rules("required").Messages("It's required").Run(gctx.New()))
|
||||
// Output:
|
||||
// It's required
|
||||
// It's required
|
||||
// It's required
|
||||
// rule deleted
|
||||
// It's required
|
||||
// <nil>
|
||||
// <nil>
|
||||
}
|
||||
|
||||
func ExampleValidator_Data_Value() {
|
||||
err := g.Validator().Rules("min:18").
|
||||
Messages("未成年人不允许注册哟").
|
||||
Data(16).Run(gctx.New())
|
||||
fmt.Println(err.String())
|
||||
|
||||
// Output:
|
||||
// 未成年人不允许注册哟
|
||||
}
|
||||
|
||||
func ExampleValidator_Data_Map3() {
|
||||
params := map[string]interface{}{
|
||||
"passport": "",
|
||||
"password": "123456",
|
||||
"password2": "1234567",
|
||||
}
|
||||
rules := map[string]string{
|
||||
"passport": "required|length:6,16",
|
||||
"password": "required|length:6,16|same:password2",
|
||||
"password2": "required|length:6,16",
|
||||
}
|
||||
messages := map[string]interface{}{
|
||||
"passport": "账号不能为空|账号长度应当在{min}到{max}之间",
|
||||
"password": map[string]string{
|
||||
"required": "密码不能为空",
|
||||
"same": "两次密码输入不相等",
|
||||
},
|
||||
}
|
||||
err := g.Validator().
|
||||
Messages(messages).
|
||||
Rules(rules).
|
||||
Data(params).Run(gctx.New())
|
||||
if err != nil {
|
||||
g.Dump(err.Maps())
|
||||
}
|
||||
|
||||
// May Output:
|
||||
// {
|
||||
// "passport": {
|
||||
// "length": "账号长度应当在6到16之间",
|
||||
// "required": "账号不能为空"
|
||||
// },
|
||||
// "password": {
|
||||
// "same": "两次密码输入不相等"
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
func ExampleValidator_Data_Struct4() {
|
||||
type User struct {
|
||||
Name string `v:"required#请输入用户姓名"`
|
||||
Type int `v:"required#请选择用户类型"`
|
||||
}
|
||||
data := g.Map{
|
||||
"name": "john",
|
||||
}
|
||||
user := User{}
|
||||
if err := gconv.Scan(data, &user); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err := g.Validator().Data(user).Assoc(data).Run(gctx.New())
|
||||
if err != nil {
|
||||
fmt.Println(err.Items())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// [map[Type:map[required:请选择用户类型]]]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user