2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-08-21 00:00:00 +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 (
|
2020-09-21 23:51:30 +08:00
|
|
|
"errors"
|
2020-08-21 00:00:00 +08:00
|
|
|
"fmt"
|
|
|
|
"github.com/gogf/gf/container/gvar"
|
2020-09-21 23:51:30 +08:00
|
|
|
"github.com/gogf/gf/frame/g"
|
|
|
|
"github.com/gogf/gf/util/gconv"
|
2020-08-21 00:00:00 +08:00
|
|
|
"github.com/gogf/gf/util/gvalid"
|
2020-09-22 08:45:22 +08:00
|
|
|
"math"
|
|
|
|
"reflect"
|
2020-08-21 00:00:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleCheckMap() {
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"passport": "",
|
|
|
|
"password": "123456",
|
|
|
|
"password2": "1234567",
|
|
|
|
}
|
|
|
|
rules := []string{
|
|
|
|
"passport@required|length:6,16#账号不能为空|账号长度应当在:min到:max之间",
|
|
|
|
"password@required|length:6,16|same:password2#密码不能为空|密码长度应当在:min到:max之间|两次密码输入不相等",
|
|
|
|
"password2@required|length:6,16#",
|
|
|
|
}
|
|
|
|
if e := gvalid.CheckMap(params, rules); e != nil {
|
|
|
|
fmt.Println(e.Map())
|
|
|
|
fmt.Println(e.FirstItem())
|
|
|
|
fmt.Println(e.FirstString())
|
|
|
|
}
|
|
|
|
// May Output:
|
|
|
|
// map[required:账号不能为空 length:账号长度应当在6到16之间]
|
|
|
|
// passport map[required:账号不能为空 length:账号长度应当在6到16之间]
|
|
|
|
// 账号不能为空
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleCheckMap2() {
|
|
|
|
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 := gvalid.CheckMap(params, rules); e != nil {
|
|
|
|
fmt.Println(e.Map())
|
|
|
|
fmt.Println(e.FirstItem())
|
|
|
|
fmt.Println(e.FirstString())
|
|
|
|
}
|
|
|
|
// Output:
|
|
|
|
// map[same:两次密码输入不相等]
|
|
|
|
// password map[same:两次密码输入不相等]
|
|
|
|
// 两次密码输入不相等
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty string attribute.
|
|
|
|
func ExampleCheckStruct() {
|
|
|
|
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 := gvalid.CheckStruct(obj, nil)
|
2020-09-22 08:45:22 +08:00
|
|
|
fmt.Println(err == nil)
|
2020-08-21 00:00:00 +08:00
|
|
|
// Output:
|
2020-09-22 08:45:22 +08:00
|
|
|
// true
|
2020-08-21 00:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Empty pointer attribute.
|
|
|
|
func ExampleCheckStruct2() {
|
|
|
|
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 := gvalid.CheckStruct(obj, nil)
|
2020-09-22 08:45:22 +08:00
|
|
|
fmt.Println(err == nil)
|
2020-08-21 00:00:00 +08:00
|
|
|
// Output:
|
2020-09-22 08:45:22 +08:00
|
|
|
// true
|
2020-08-21 00:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Empty integer attribute.
|
|
|
|
func ExampleCheckStruct3() {
|
|
|
|
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 := gvalid.CheckStruct(obj, nil)
|
|
|
|
fmt.Println(err)
|
|
|
|
// Output:
|
|
|
|
// project id must between 1, 10000
|
|
|
|
}
|
2020-09-21 23:51:30 +08:00
|
|
|
|
|
|
|
func ExampleRegisterRule() {
|
|
|
|
rule := "unique-name"
|
2020-09-29 23:25:20 +08:00
|
|
|
gvalid.RegisterRule(rule, func(rule string, value interface{}, message string, params map[string]interface{}) error {
|
2020-09-21 23:51:30 +08:00
|
|
|
var (
|
|
|
|
id = gconv.Int(params["Id"])
|
|
|
|
name = gconv.String(value)
|
|
|
|
)
|
|
|
|
n, err := g.Table("user").Where("id != ? and name = ?", id, name).Count()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n > 0 {
|
|
|
|
return errors.New(message)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
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",
|
|
|
|
}
|
|
|
|
err := gvalid.CheckStruct(user, nil)
|
|
|
|
fmt.Println(err.Error())
|
2020-09-22 08:45:22 +08:00
|
|
|
// May Output:
|
2020-09-21 23:51:30 +08:00
|
|
|
// 用户名称已被占用
|
|
|
|
}
|
2020-09-22 08:45:22 +08:00
|
|
|
|
|
|
|
func ExampleRegisterRule_OverwriteRequired() {
|
|
|
|
rule := "required"
|
2020-09-29 23:25:20 +08:00
|
|
|
gvalid.RegisterRule(rule, func(rule string, value interface{}, message string, params map[string]interface{}) error {
|
2020-09-22 08:45:22 +08:00
|
|
|
reflectValue := reflect.ValueOf(value)
|
|
|
|
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(message)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
fmt.Println(gvalid.Check("", "required", "It's required"))
|
|
|
|
fmt.Println(gvalid.Check([]string{}, "required", "It's required"))
|
|
|
|
fmt.Println(gvalid.Check(map[string]int{}, "required", "It's required"))
|
|
|
|
gvalid.DeleteRule(rule)
|
|
|
|
fmt.Println("rule deleted")
|
|
|
|
fmt.Println(gvalid.Check("", "required", "It's required"))
|
|
|
|
fmt.Println(gvalid.Check([]string{}, "required", "It's required"))
|
|
|
|
fmt.Println(gvalid.Check(map[string]int{}, "required", "It's required"))
|
|
|
|
// Output:
|
|
|
|
// It's required
|
|
|
|
// It's required
|
|
|
|
// It's required
|
|
|
|
// rule deleted
|
|
|
|
// It's required
|
|
|
|
}
|