GoFrame框架校验组件内置了数十项常用的校验规则,校验组件是开发者最频繁使用的框架核心组件之一。

校验规则涉及到联合校验的场景时,规则中关联的参数名称会自动按照不区分大小写且忽略特殊字符的形式进行智能匹配。以下示例代码中我们均采用常用的结构体校验的方式,结构体属性中的v标签标识validation的缩写,用于设置该属性的校验规格。

修饰规则

修饰规则本身不做任何的校验逻辑,而是修改功能规则的实现逻辑。

bail

  • 格式:bail
  • 说明:只要后续的多个校验中有一个规则校验失败则停止校验并立即返回校验结果。
  • 注意:在框架的HTTP Server组件中,如果采用规范路由注册方式,在其自动校验特性中将会自动开启bail修饰规则,开发者无需在Req对象中显式设置bail
  • 示例:

    1. func Example_Rule_Bail() {
    2. type BizReq struct {
    3. Account string `v:"bail|required|length:6,16|same:QQ"`
    4. QQ string
    5. Password string `v:"required|same:Password2"`
    6. Password2 string `v:"required"`
    7. }
    8. var (
    9. ctx = context.Background()
    10. req = BizReq{
    11. Account: "gf",
    12. QQ: "123456",
    13. Password: "goframe.org",
    14. Password2: "goframe.org",
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Println(err)
    19. }
    20. // output:
    21. // The Account value `gf` length must be between 6 and 16
    22. }

ci

  • 格式: ci
  • 说明:字段值比较在默认情况下为区分大小写严格匹配比较,通过Case Insensitive,可以设置对于需要比较值的规则字段为不区分大小写。如:same, different, in, not-in等。
  • 示例:

    1. func Example_Rule_CaseInsensitive() {
    2. type BizReq struct {
    3. Account string `v:"required"`
    4. Password string `v:"required|ci|same:Password2"`
    5. Password2 string `v:"required"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Account: "gf",
    11. Password: "Goframe.org", // Diff from Password2, but because of "ci", rule check passed
    12. Password2: "goframe.org",
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Println(err)
    17. }
    18. // output:
    19. }

foreach

  • 格式:foreach
  • 说明:将待检验的参数作为数组遍历,并将后一个校验规则应用于数组中的每一项。
  • 版本:框架版本>=v2.2.0
  • 示例:

    1. func Example_Rule_Foreach() {
    2. type BizReq struct {
    3. Value1 []int `v:"foreach|in:1,2,3"`
    4. Value2 []int `v:"foreach|in:1,2,3"`
    5. }
    6. var (
    7. ctx = context.Background()
    8. req = BizReq{
    9. Value1: []int{1, 2, 3},
    10. Value2: []int{3, 4, 5},
    11. }
    12. )
    13. if err := g.Validator().Bail().Data(req).Run(ctx); err != nil {
    14. fmt.Println(err.String())
    15. }
    16. // Output:
    17. // The Value2 value `4` is not in acceptable range: 1,2,3
    18. }

功能规则

功能规则实现特定的校验逻辑,框架内置了非常丰富强大的内置校验规则。

required

  • 格式: required
  • 说明:必需参数,除了支持常见的字符串,也支持Slice/Map类型。
  • 示例:姓名字段Name为必需参数必需不能为空。

    1. func Example_Rule_Required() {
    2. type BizReq struct {
    3. ID uint `v:"required"`
    4. Name string `v:"required"`
    5. }
    6. var (
    7. ctx = context.Background()
    8. req = BizReq{
    9. ID: 1,
    10. }
    11. )
    12. if err := g.Validator().Data(req).Run(ctx); err != nil {
    13. fmt.Print(err)
    14. }
    15. // Output:
    16. // The Name field is required
    17. }

required-if

  • 格式: required-if:field,value,...
  • 说明:必需参数(当任意所给定字段值与所给值相等时,即:当field字段的值为value时,当前验证字段为必须参数)。多个字段以,号分隔。
  • 示例:当Gender字段为1WifeName字段必须不为空, 当Gender字段为2HusbandName字段必须不为空。

    1. func Example_Rule_RequiredIf() {
    2. type BizReq struct {
    3. ID uint `v:"required" dc:"Your ID"`
    4. Name string `v:"required" dc:"Your name"`
    5. Gender uint `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"`
    6. WifeName string `v:"required-if:gender,1"`
    7. HusbandName string `v:"required-if:gender,2"`
    8. }
    9. var (
    10. ctx = context.Background()
    11. req = BizReq{
    12. ID: 1,
    13. Name: "test",
    14. Gender: 1,
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Println(err)
    19. }
    20. // Output:
    21. // The WifeName field is required
    22. }

required-unless

  • 格式: required-unless:field,value,...
  • 说明:必需参数(当所给定字段值与所给值都不相等时,即:当field字段的值不为value时,当前验证字段为必须参数)。多个字段以,号分隔。
  • 示例:当Gender不等于0Gender不等于2时,WifeName必须不为空;当Id 不等于0Gender 不等于2时, HusbandName 必须不为空。

    1. func Example_Rule_RequiredUnless() {
    2. type BizReq struct {
    3. ID uint `v:"required" dc:"Your ID"`
    4. Name string `v:"required" dc:"Your name"`
    5. Gender uint `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"`
    6. WifeName string `v:"required-unless:gender,0,gender,2"`
    7. HusbandName string `v:"required-unless:id,0,gender,2"`
    8. }
    9. var (
    10. ctx = context.Background()
    11. req = BizReq{
    12. ID: 1,
    13. Name: "test",
    14. Gender: 1,
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Println(err)
    19. }
    20. // Output:
    21. // The WifeName field is required; The HusbandName field is required
    22. }

required-with

  • 格式: required-with:field1,field2,...
  • 说明:必需参数(当所给定任意字段值其中之一不为空时)。
  • 示例:当WifeName不为空时,HusbandName必须不为空。

    1. func Example_Rule_RequiredWith() {
    2. type BizReq struct {
    3. ID uint `v:"required" dc:"Your ID"`
    4. Name string `v:"required" dc:"Your name"`
    5. Gender uint `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"`
    6. WifeName string
    7. HusbandName string `v:"required-with:WifeName"`
    8. }
    9. var (
    10. ctx = context.Background()
    11. req = BizReq{
    12. ID: 1,
    13. Name: "test",
    14. Gender: 1,
    15. WifeName: "Ann",
    16. }
    17. )
    18. if err := g.Validator().Data(req).Run(ctx); err != nil {
    19. fmt.Println(err)
    20. }
    21. // Output:
    22. // The HusbandName field is required
    23. }

required-with-all

  • 格式: required-with-all:field1,field2,...
  • 说明:必须参数(当所给定所有字段值全部都不为空时)。
  • 示例:当Id,Name,Gender,WifeName全部不为空时,HusbandName必须不为空。

    1. func Example_Rule_RequiredWithAll() {
    2. type BizReq struct {
    3. ID uint `v:"required" dc:"Your ID"`
    4. Name string `v:"required" dc:"Your name"`
    5. Gender uint `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"`
    6. WifeName string
    7. HusbandName string `v:"required-with-all:Id,Name,Gender,WifeName"`
    8. }
    9. var (
    10. ctx = context.Background()
    11. req = BizReq{
    12. ID: 1,
    13. Name: "test",
    14. Gender: 1,
    15. WifeName: "Ann",
    16. }
    17. )
    18. if err := g.Validator().Data(req).Run(ctx); err != nil {
    19. fmt.Println(err)
    20. }
    21. // Output:
    22. // The HusbandName field is required
    23. }

required-without

  • 格式: required-without:field1,field2,...
  • 说明:必需参数(当所给定任意字段值其中之一为空时)。
  • 示例:当IdWifeName为空时,HusbandName必须不为空。

    1. func Example_Rule_RequiredWithout() {
    2. type BizReq struct {
    3. ID uint `v:"required" dc:"Your ID"`
    4. Name string `v:"required" dc:"Your name"`
    5. Gender uint `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"`
    6. WifeName string
    7. HusbandName string `v:"required-without:Id,WifeName"`
    8. }
    9. var (
    10. ctx = context.Background()
    11. req = BizReq{
    12. ID: 1,
    13. Name: "test",
    14. Gender: 1,
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Println(err)
    19. }
    20. // Output:
    21. // The HusbandName field is required
    22. }

required-without-all

  • 格式: required-without-all:field1,field2,...
  • 说明:必须参数(当所给定所有字段值全部都为空时)。
  • 示例:当IdWifeName都为空时,HusbandName必须不为空。

    1. func Example_Rule_RequiredWithoutAll() {
    2. type BizReq struct {
    3. ID uint `v:"required" dc:"Your ID"`
    4. Name string `v:"required" dc:"Your name"`
    5. Gender uint `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"`
    6. WifeName string
    7. HusbandName string `v:"required-without-all:Id,WifeName"`
    8. }
    9. var (
    10. ctx = context.Background()
    11. req = BizReq{
    12. Name: "test",
    13. Gender: 1,
    14. }
    15. )
    16. if err := g.Validator().Data(req).Run(ctx); err != nil {
    17. fmt.Println(err)
    18. }
    19. // Output:
    20. // The HusbandName field is required
    21. }

date

  • 格式: date
  • 说明:参数为常用日期类型,日期之间支持的连接符号-/.,也支持不带连接符号的8位长度日期,格式如: 2006-01-02, 2006/01/02, 2006.01.02, 20060102
  • 示例:

    1. func Example_Rule_Date() {
    2. type BizReq struct {
    3. Date1 string `v:"date"`
    4. Date2 string `v:"date"`
    5. Date3 string `v:"date"`
    6. Date4 string `v:"date"`
    7. Date5 string `v:"date"`
    8. }
    9. var (
    10. ctx = context.Background()
    11. req = BizReq{
    12. Date1: "2021-10-31",
    13. Date2: "2021.10.31",
    14. Date3: "2021-Oct-31",
    15. Date4: "2021 Octa 31",
    16. Date5: "2021/Oct/31",
    17. }
    18. )
    19. if err := g.Validator().Data(req).Run(ctx); err != nil {
    20. fmt.Print(gstr.Join(err.Strings(), "\n"))
    21. }
    22. // Output:
    23. // The Date3 value `2021-Oct-31` is not a valid date
    24. // The Date4 value `2021 Octa 31` is not a valid date
    25. // The Date5 value `2021/Oct/31` is not a valid date
    26. }

datetime

  • 格式: datetime
  • 说明:参数为常用日期时间类型,其中日期之间支持的连接符号只支持-,格式如: 2006-01-02 12:00:00
  • 示例:

    1. func Example_Rule_Datetime() {
    2. type BizReq struct {
    3. Date1 string `v:"datetime"`
    4. Date2 string `v:"datetime"`
    5. Date3 string `v:"datetime"`
    6. Date4 string `v:"datetime"`
    7. }
    8. var (
    9. ctx = context.Background()
    10. req = BizReq{
    11. Date1: "2021-11-01 23:00:00",
    12. Date2: "2021-11-01 23:00", // error
    13. Date3: "2021/11/01 23:00:00", // error
    14. Date4: "2021/Dec/01 23:00:00", // error
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Print(gstr.Join(err.Strings(), "\n"))
    19. }
    20. // Output:
    21. // The Date2 value `2021-11-01 23:00` is not a valid datetime
    22. // The Date3 value `2021/11/01 23:00:00` is not a valid datetime
    23. // The Date4 value `2021/Dec/01 23:00:00` is not a valid datetime
    24. }

date-format

  • 格式: date-format:format
  • 说明:判断日期是否为指定的日期/时间格式,format参数格式为gtime日期格式(可以包含日期及时间),格式说明参考章节:gtime模块
  • 示例:date-format:Y-m-d H:i:s

    1. func Example_Rule_DateFormat() {
    2. type BizReq struct {
    3. Date1 string `v:"date-format:Y-m-d"`
    4. Date2 string `v:"date-format:Y-m-d"`
    5. Date3 string `v:"date-format:Y-m-d H:i:s"`
    6. Date4 string `v:"date-format:Y-m-d H:i:s"`
    7. }
    8. var (
    9. ctx = context.Background()
    10. req = BizReq{
    11. Date1: "2021-11-01",
    12. Date2: "2021-11-01 23:00", // error
    13. Date3: "2021-11-01 23:00:00",
    14. Date4: "2021-11-01 23:00", // error
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Print(gstr.Join(err.Strings(), "\n"))
    19. }
    20. // Output:
    21. // The Date2 value `2021-11-01 23:00` does not match the format: Y-m-d
    22. // The Date4 value `2021-11-01 23:00` does not match the format: Y-m-d H:i:s
    23. }

before

  • 格式: before:field
  • 说明:判断给定的日期/时间是否在指定字段的日期/时间之前。

  • 版本:框架版本>=v2.2.0

    1. func Example_Rule_Before() {
    2. type BizReq struct {
    3. Time1 string `v:"before:Time3"`
    4. Time2 string `v:"before:Time3"`
    5. Time3 string
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Time1: "2022-09-02",
    11. Time2: "2022-09-03",
    12. Time3: "2022-09-03",
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Println(err.String())
    17. }
    18. // Output:
    19. // The Time2 value `2022-09-03` must be before field Time3 value `2022-09-03`
    20. }

before-equal

  • 格式: before-equal:field
  • 说明:判断给定的日期/时间是否在指定字段的日期/时间之前,或者与指定字段的日期/时间相等。

  • 版本:框架版本>=v2.2.0

    1. func Example_Rule_BeforeEqual() {
    2. type BizReq struct {
    3. Time1 string `v:"before-equal:Time3"`
    4. Time2 string `v:"before-equal:Time3"`
    5. Time3 string
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Time1: "2022-09-02",
    11. Time2: "2022-09-01",
    12. Time3: "2022-09-01",
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Print(gstr.Join(err.Strings(), "\n"))
    17. }
    18. // Output:
    19. // The Time1 value `2022-09-02` must be before or equal to field Time3
    20. }

after

  • 格式: after:field
  • 说明:判断给定的日期/时间是否在指定字段的日期/时间之后。

  • 版本:框架版本>=v2.2.0

    1. func Example_Rule_After() {
    2. type BizReq struct {
    3. Time1 string
    4. Time2 string `v:"after:Time1"`
    5. Time3 string `v:"after:Time1"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Time1: "2022-09-01",
    11. Time2: "2022-09-01",
    12. Time3: "2022-09-02",
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Println(err.String())
    17. }
    18. // Output:
    19. // The Time2 value `2022-09-01` must be after field Time1 value `2022-09-01`
    20. }

after-equal

  • 格式: after-equal:field
  • 说明:判断给定的日期/时间是否在指定字段的日期/时间之后,或者与指定字段的日期/时间相等。

  • 版本:框架版本>=v2.2.0

    1. func Example_Rule_AfterEqual() {
    2. type BizReq struct {
    3. Time1 string
    4. Time2 string `v:"after-equal:Time1"`
    5. Time3 string `v:"after-equal:Time1"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Time1: "2022-09-02",
    11. Time2: "2022-09-01",
    12. Time3: "2022-09-02",
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Print(gstr.Join(err.Strings(), "\n"))
    17. }
    18. // Output:
    19. // The Time2 value `2022-09-01` must be after or equal to field Time1 value `2022-09-02`
    20. }

array

  • 格式: array
  • 说明:判断给定的参数是否数组格式。如果给定的参数为JSON数组字符串,也将检验通过。

  • 版本:框架版本>=v2.2.0

    1. func Example_Rule_Array() {
    2. type BizReq struct {
    3. Value1 string `v:"array"`
    4. Value2 string `v:"array"`
    5. Value3 string `v:"array"`
    6. Value4 []string `v:"array"`
    7. }
    8. var (
    9. ctx = context.Background()
    10. req = BizReq{
    11. Value1: "1,2,3",
    12. Value2: "[]",
    13. Value3: "[1,2,3]",
    14. Value4: []string{},
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Print(gstr.Join(err.Strings(), "\n"))
    19. }
    20. // Output:
    21. // The Value1 value `1,2,3` is not of valid array type
    22. }

email

  • 格式:email
  • 说明:EMAIL邮箱地址格式。

    1. func Example_Rule_Email() {
    2. type BizReq struct {
    3. MailAddr1 string `v:"email"`
    4. MailAddr2 string `v:"email"`
    5. MailAddr3 string `v:"email"`
    6. MailAddr4 string `v:"email"`
    7. }
    8. var (
    9. ctx = context.Background()
    10. req = BizReq{
    11. MailAddr1: "gf@goframe.org",
    12. MailAddr2: "gf@goframe", // error
    13. MailAddr3: "gf@goframe.org.cn",
    14. MailAddr4: "gf#goframe.org", // error
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Print(gstr.Join(err.Strings(), "\n"))
    19. }
    20. // Output:
    21. // The MailAddr2 value `gf@goframe` is not a valid email address
    22. // The MailAddr4 value `gf#goframe.org` is not a valid email address
    23. }

phone

  • 格式:phone
  • 说明:大中国区手机号格式。

    1. func Example_Rule_Phone() {
    2. type BizReq struct {
    3. PhoneNumber1 string `v:"phone"`
    4. PhoneNumber2 string `v:"phone"`
    5. PhoneNumber3 string `v:"phone"`
    6. PhoneNumber4 string `v:"phone"`
    7. }
    8. var (
    9. ctx = context.Background()
    10. req = BizReq{
    11. PhoneNumber1: "13578912345",
    12. PhoneNumber2: "11578912345", // error 11x not exist
    13. PhoneNumber3: "17178912345", // error 171 not exit
    14. PhoneNumber4: "1357891234", // error len must be 11
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Print(gstr.Join(err.Strings(), "\n"))
    19. }
    20. // Output:
    21. // The PhoneNumber2 value `11578912345` is not a valid phone number
    22. // The PhoneNumber3 value `17178912345` is not a valid phone number
    23. // The PhoneNumber4 value `1357891234` is not a valid phone number
    24. }

phone-loose

  • 格式: phone
  • 说明:宽松的手机号验证,只要满足 13、14、15、16、17、18、19开头的11位数字都可以通过验证。可用于非严格的业务场景。

    1. func Example_Rule_PhoneLoose() {
    2. type BizReq struct {
    3. PhoneNumber1 string `v:"phone-loose"`
    4. PhoneNumber2 string `v:"phone-loose"`
    5. PhoneNumber3 string `v:"phone-loose"`
    6. PhoneNumber4 string `v:"phone-loose"`
    7. }
    8. var (
    9. ctx = context.Background()
    10. req = BizReq{
    11. PhoneNumber1: "13578912345",
    12. PhoneNumber2: "11578912345", // error 11x not exist
    13. PhoneNumber3: "17178912345",
    14. PhoneNumber4: "1357891234", // error len must be 11
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Print(gstr.Join(err.Strings(), "\n"))
    19. }
    20. // Output:
    21. // The PhoneNumber2 value `11578912345` is invalid
    22. // The PhoneNumber4 value `1357891234` is invalid
    23. }

telephone

  • 格式: telephone
  • 说明:大中国区座机电话号码,”XXXX-XXXXXXX”、”XXXX-XXXXXXXX”、”XXX-XXXXXXX”、”XXX-XXXXXXXX”、”XXXXXXX”、”XXXXXXXX”。

    1. func Example_Rule_Telephone() {
    2. type BizReq struct {
    3. Telephone1 string `v:"telephone"`
    4. Telephone2 string `v:"telephone"`
    5. Telephone3 string `v:"telephone"`
    6. Telephone4 string `v:"telephone"`
    7. }
    8. var (
    9. ctx = context.Background()
    10. req = BizReq{
    11. Telephone1: "010-77542145",
    12. Telephone2: "0571-77542145",
    13. Telephone3: "20-77542145", // error
    14. Telephone4: "775421451", // error len must be 7 or 8
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Print(gstr.Join(err.Strings(), "\n"))
    19. }
    20. // Output:
    21. // The Telephone3 value `20-77542145` is not a valid telephone number
    22. // The Telephone4 value `775421451` is not a valid telephone number
    23. }

passport

  • 格式: passport
  • 说明:通用帐号规则(字母开头,只能包含字母、数字和下划线,长度在6~18之间)。

    1. func Example_Rule_Passport() {
    2. type BizReq struct {
    3. Passport1 string `v:"passport"`
    4. Passport2 string `v:"passport"`
    5. Passport3 string `v:"passport"`
    6. Passport4 string `v:"passport"`
    7. }
    8. var (
    9. ctx = context.Background()
    10. req = BizReq{
    11. Passport1: "goframe",
    12. Passport2: "1356666", // error starting with letter
    13. Passport3: "goframe#", // error containing only numbers or underscores
    14. Passport4: "gf", // error length between 6 and 18
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Print(gstr.Join(err.Strings(), "\n"))
    19. }
    20. // Output:
    21. // The Passport2 value `1356666` is not a valid passport format
    22. // The Passport3 value `goframe#` is not a valid passport format
    23. // The Passport4 value `gf` is not a valid passport format
    24. }

password

  • 格式: password
  • 说明:通用密码规则(任意可见字符,长度在6~18之间)。

    1. func Example_Rule_Password() {
    2. type BizReq struct {
    3. Password1 string `v:"password"`
    4. Password2 string `v:"password"`
    5. }
    6. var (
    7. ctx = context.Background()
    8. req = BizReq{
    9. Password1: "goframe",
    10. Password2: "gofra", // error length between 6 and 18
    11. }
    12. )
    13. if err := g.Validator().Data(req).Run(ctx); err != nil {
    14. fmt.Print(err)
    15. }
    16. // Output:
    17. // The Password2 value `gofra` is not a valid password format
    18. }

password2

  • 格式: password2
  • 说明:中等强度密码(在通用密码规则的基础上,要求密码必须包含大小写字母和数字)。

    1. func Example_Rule_Password2() {
    2. type BizReq struct {
    3. Password1 string `v:"password2"`
    4. Password2 string `v:"password2"`
    5. Password3 string `v:"password2"`
    6. Password4 string `v:"password2"`
    7. }
    8. var (
    9. ctx = context.Background()
    10. req = BizReq{
    11. Password1: "Goframe123",
    12. Password2: "gofra", // error length between 6 and 18
    13. Password3: "Goframe", // error must contain lower and upper letters and numbers.
    14. Password4: "goframe123", // error must contain lower and upper letters and numbers.
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Print(gstr.Join(err.Strings(), "\n"))
    19. }
    20. // Output:
    21. // The Password2 value `gofra` is not a valid password format
    22. // The Password3 value `Goframe` is not a valid password format
    23. // The Password4 value `goframe123` is not a valid password format
    24. }

password3

  • 格式: password3
  • 说明:强等强度密码(在通用密码规则的基础上,必须包含大小写字母、数字和特殊字符)。

    1. func Example_Rule_Password3() {
    2. type BizReq struct {
    3. Password1 string `v:"password3"`
    4. Password2 string `v:"password3"`
    5. Password3 string `v:"password3"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Password1: "Goframe123#",
    11. Password2: "gofra", // error length between 6 and 18
    12. Password3: "Goframe123", // error must contain lower and upper letters, numbers and special chars.
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Print(gstr.Join(err.Strings(), "\n"))
    17. }
    18. // Output:
    19. // The Password2 value `gofra` is not a valid password format
    20. // The Password3 value `Goframe123` is not a valid password format
    21. }

postcode

  • 格式: postcode
  • 说明:大中国区邮政编码规则。

    1. func Example_Rule_Postcode() {
    2. type BizReq struct {
    3. Postcode1 string `v:"postcode"`
    4. Postcode2 string `v:"postcode"`
    5. Postcode3 string `v:"postcode"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Postcode1: "100000",
    11. Postcode2: "10000", // error length must be 6
    12. Postcode3: "1000000", // error length must be 6
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Print(gstr.Join(err.Strings(), "\n"))
    17. }
    18. // Output:
    19. // The Postcode2 value `10000` is not a valid postcode format
    20. // The Postcode3 value `1000000` is not a valid postcode format
    21. }

resident-id

  • 格式: resident-id
  • 说明:公民身份证号码。

    1. func Example_Rule_ResidentId() {
    2. type BizReq struct {
    3. ResidentID1 string `v:"resident-id"`
    4. }
    5. var (
    6. ctx = context.Background()
    7. req = BizReq{
    8. ResidentID1: "320107199506285482",
    9. }
    10. )
    11. if err := g.Validator().Data(req).Run(ctx); err != nil {
    12. fmt.Print(err)
    13. }
    14. // Output:
    15. // The ResidentID1 value `320107199506285482` is not a valid resident id number
    16. }

bank-card

  • 格式: bank-card
  • 说明:大中国区银行卡号校验。

    1. func Example_Rule_BankCard() {
    2. type BizReq struct {
    3. BankCard1 string `v:"bank-card"`
    4. }
    5. var (
    6. ctx = context.Background()
    7. req = BizReq{
    8. BankCard1: "6225760079930218",
    9. }
    10. )
    11. if err := g.Validator().Data(req).Run(ctx); err != nil {
    12. fmt.Print(err)
    13. }
    14. // Output:
    15. // The BankCard1 value `6225760079930218` is not a valid bank card number
    16. }

qq

  • 格式: qq
  • 说明:腾讯QQ号码规则。

    1. func Example_Rule_QQ() {
    2. type BizReq struct {
    3. QQ1 string `v:"qq"`
    4. QQ2 string `v:"qq"`
    5. QQ3 string `v:"qq"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. QQ1: "389961817",
    11. QQ2: "9999", // error >= 10000
    12. QQ3: "514258412a", // error all number
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Print(gstr.Join(err.Strings(), "\n"))
    17. }
    18. // Output:
    19. // The QQ2 value `9999` is not a valid QQ number
    20. // The QQ3 value `514258412a` is not a valid QQ number
    21. }

ip

  • 格式: ip
  • 说明:IPv4/IPv6地址。

    1. func Example_Rule_IP() {
    2. type BizReq struct {
    3. IP1 string `v:"ip"`
    4. IP2 string `v:"ip"`
    5. IP3 string `v:"ip"`
    6. IP4 string `v:"ip"`
    7. }
    8. var (
    9. ctx = context.Background()
    10. req = BizReq{
    11. IP1: "127.0.0.1",
    12. IP2: "fe80::812b:1158:1f43:f0d1",
    13. IP3: "520.255.255.255", // error >= 10000
    14. IP4: "ze80::812b:1158:1f43:f0d1",
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Print(gstr.Join(err.Strings(), "\n"))
    19. }
    20. // Output:
    21. // The IP3 value `520.255.255.255` is not a valid IP address
    22. // The IP4 value `ze80::812b:1158:1f43:f0d1` is not a valid IP address
    23. }

ipv4

  • 格式: ipv4
  • 说明:IPv4地址。

    1. func Example_Rule_IPV4() {
    2. type BizReq struct {
    3. IP1 string `v:"ipv4"`
    4. IP2 string `v:"ipv4"`
    5. }
    6. var (
    7. ctx = context.Background()
    8. req = BizReq{
    9. IP1: "127.0.0.1",
    10. IP2: "520.255.255.255",
    11. }
    12. )
    13. if err := g.Validator().Data(req).Run(ctx); err != nil {
    14. fmt.Print(err)
    15. }
    16. // Output:
    17. // The IP2 value `520.255.255.255` is not a valid IPv4 address
    18. }

ipv6

  • 格式: ipv6
  • 说明:IPv6地址。

    1. func Example_Rule_IPV6() {
    2. type BizReq struct {
    3. IP1 string `v:"ipv6"`
    4. IP2 string `v:"ipv6"`
    5. }
    6. var (
    7. ctx = context.Background()
    8. req = BizReq{
    9. IP1: "fe80::812b:1158:1f43:f0d1",
    10. IP2: "ze80::812b:1158:1f43:f0d1",
    11. }
    12. )
    13. if err := g.Validator().Data(req).Run(ctx); err != nil {
    14. fmt.Print(err)
    15. }
    16. // Output:
    17. // The IP2 value `ze80::812b:1158:1f43:f0d1` is not a valid IPv6 address
    18. }

mac

  • 格式: mac
  • 说明:MAC地址。

    1. func Example_Rule_Mac() {
    2. type BizReq struct {
    3. Mac1 string `v:"mac"`
    4. Mac2 string `v:"mac"`
    5. }
    6. var (
    7. ctx = context.Background()
    8. req = BizReq{
    9. Mac1: "4C-CC-6A-D6-B1-1A",
    10. Mac2: "Z0-CC-6A-D6-B1-1A",
    11. }
    12. )
    13. if err := g.Validator().Data(req).Run(ctx); err != nil {
    14. fmt.Print(err)
    15. }
    16. // Output:
    17. // The Mac2 value `Z0-CC-6A-D6-B1-1A` is not a valid MAC address
    18. }

url

  • 格式: url
  • 说明:URL
  • 示例:支持以http,https,ftp,file开头的地址。

    1. func Example_Rule_Url() {
    2. type BizReq struct {
    3. URL1 string `v:"url"`
    4. URL2 string `v:"url"`
    5. URL3 string `v:"url"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. URL1: "http://goframe.org",
    11. URL2: "ftp://goframe.org",
    12. URL3: "ws://goframe.org",
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Print(err)
    17. }
    18. // Output:
    19. // The URL3 value `ws://goframe.org` is not a valid URL address
    20. }

domain

  • 格式: domain
  • 说明:域名
  • 示例:域名规则。xxx.yyy(首位必须为字母)。

    1. func Example_Rule_Domain() {
    2. type BizReq struct {
    3. Domain1 string `v:"domain"`
    4. Domain2 string `v:"domain"`
    5. Domain3 string `v:"domain"`
    6. Domain4 string `v:"domain"`
    7. }
    8. var (
    9. ctx = context.Background()
    10. req = BizReq{
    11. Domain1: "goframe.org",
    12. Domain2: "a.b",
    13. Domain3: "goframe#org",
    14. Domain4: "1a.2b",
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Print(gstr.Join(err.Strings(), "\n"))
    19. }
    20. // Output:
    21. // The Domain3 value `goframe#org` is not a valid domain format
    22. // The Domain4 value `1a.2b` is not a valid domain format
    23. }

size

  • 格式: size:size
  • 说明:参数长度size (长度参数为整形),注意底层使用Unicode计算长度,因此中文一个汉字占1个长度单位。

    1. func Example_Rule_Size() {
    2. type BizReq struct {
    3. Size1 string `v:"size:10"`
    4. Size2 string `v:"size:5"`
    5. }
    6. var (
    7. ctx = context.Background()
    8. req = BizReq{
    9. Size1: "goframe欢迎你",
    10. Size2: "goframe",
    11. }
    12. )
    13. if err := g.Validator().Data(req).Run(ctx); err != nil {
    14. fmt.Print(err)
    15. }
    16. // Output:
    17. // The Size2 value `goframe` length must be 5
    18. }

length

  • 格式: length:min,max
  • 说明:参数长度minmax(长度参数为整形),注意底层使用Unicode计算长度,因此中文一个汉字占1个长度单位。

    1. func Example_Rule_Length() {
    2. type BizReq struct {
    3. Length1 string `v:"length:5,10"`
    4. Length2 string `v:"length:10,15"`
    5. }
    6. var (
    7. ctx = context.Background()
    8. req = BizReq{
    9. Length1: "goframe欢迎你",
    10. Length2: "goframe",
    11. }
    12. )
    13. if err := g.Validator().Data(req).Run(ctx); err != nil {
    14. fmt.Print(err)
    15. }
    16. // Output:
    17. // The Length2 value `goframe` length must be between 10 and 15
    18. }

min-length

  • 格式: min-length:min
  • 说明:参数长度最小为min(长度参数为整形),注意底层使用Unicode计算长度,因此中文一个汉字占1个长度单位。

    1. func Example_Rule_MinLength() {
    2. type BizReq struct {
    3. MinLength1 string `v:"min-length:10"`
    4. MinLength2 string `v:"min-length:8"`
    5. }
    6. var (
    7. ctx = context.Background()
    8. req = BizReq{
    9. MinLength1: "goframe欢迎你",
    10. MinLength2: "goframe",
    11. }
    12. )
    13. if err := g.Validator().Data(req).Run(ctx); err != nil {
    14. fmt.Print(err)
    15. }
    16. // Output:
    17. // The MinLength2 value `goframe` length must be equal or greater than 8
    18. }

max-length

  • 格式: max-length:max
  • 说明:参数长度最大为max(长度参数为整形),注意底层使用Unicode计算长度,因此中文一个汉字占1个长度单位。

    1. func Example_Rule_MaxLength() {
    2. type BizReq struct {
    3. MaxLength1 string `v:"max-length:10"`
    4. MaxLength2 string `v:"max-length:5"`
    5. }
    6. var (
    7. ctx = context.Background()
    8. req = BizReq{
    9. MaxLength1: "goframe欢迎你",
    10. MaxLength2: "goframe",
    11. }
    12. )
    13. if err := g.Validator().Data(req).Run(ctx); err != nil {
    14. fmt.Print(err)
    15. }
    16. // Output:
    17. // The MaxLength2 value `goframe` length must be equal or lesser than 5
    18. }

between

  • 格式: between:min,max
  • 说明:参数大小minmax(支持整形和浮点类型参数)。

    1. func Example_Rule_Between() {
    2. type BizReq struct {
    3. Age1 int `v:"between:1,100"`
    4. Age2 int `v:"between:1,100"`
    5. Score1 float32 `v:"between:0.0,10.0"`
    6. Score2 float32 `v:"between:0.0,10.0"`
    7. }
    8. var (
    9. ctx = context.Background()
    10. req = BizReq{
    11. Age1: 50,
    12. Age2: 101,
    13. Score1: 9.8,
    14. Score2: -0.5,
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Print(gstr.Join(err.Strings(), "\n"))
    19. }
    20. // Output:
    21. // The Age2 value `101` must be between 1 and 100
    22. // The Score2 value `-0.5` must be between 0 and 10
    23. }

min

  • 格式: min:min
  • 说明:参数大小最小为min(支持整形和浮点类型参数)。

    1. func Example_Rule_Min() {
    2. type BizReq struct {
    3. Age1 int `v:"min:100"`
    4. Age2 int `v:"min:100"`
    5. Score1 float32 `v:"min:10.0"`
    6. Score2 float32 `v:"min:10.0"`
    7. }
    8. var (
    9. ctx = context.Background()
    10. req = BizReq{
    11. Age1: 50,
    12. Age2: 101,
    13. Score1: 9.8,
    14. Score2: 10.1,
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Print(gstr.Join(err.Strings(), "\n"))
    19. }
    20. // Output:
    21. // The Age1 value `50` must be equal or greater than 100
    22. // The Score1 value `9.8` must be equal or greater than 10
    23. }

max

  • 格式: max:max
  • 说明:参数大小最大为max(支持整形和浮点类型参数)。

    1. func Example_Rule_Max() {
    2. type BizReq struct {
    3. Age1 int `v:"max:100"`
    4. Age2 int `v:"max:100"`
    5. Score1 float32 `v:"max:10.0"`
    6. Score2 float32 `v:"max:10.0"`
    7. }
    8. var (
    9. ctx = context.Background()
    10. req = BizReq{
    11. Age1: 99,
    12. Age2: 101,
    13. Score1: 9.9,
    14. Score2: 10.1,
    15. }
    16. )
    17. if err := g.Validator().Data(req).Run(ctx); err != nil {
    18. fmt.Print(gstr.Join(err.Strings(), "\n"))
    19. }
    20. // Output:
    21. // The Age2 value `101` must be equal or lesser than 100
    22. // The Score2 value `10.1` must be equal or lesser than 10
    23. }

json

  • 格式: json
  • 说明:判断数据格式为JSON

    1. func Example_Rule_Json() {
    2. type BizReq struct {
    3. JSON1 string `v:"json"`
    4. JSON2 string `v:"json"`
    5. }
    6. var (
    7. ctx = context.Background()
    8. req = BizReq{
    9. JSON1: "{\"name\":\"goframe\",\"author\":\"郭强\"}",
    10. JSON2: "{\"name\":\"goframe\",\"author\":\"郭强\",\"test\"}",
    11. }
    12. )
    13. if err := g.Validator().Data(req).Run(ctx); err != nil {
    14. fmt.Print(err)
    15. }
    16. // Output:
    17. // The JSON2 value `{"name":"goframe","author":"郭强","test"}` is not a valid JSON string
    18. }

integer

  • 格式: integer
  • 说明:整数(正整数或者负整数)。

    1. func Example_Rule_Integer() {
    2. type BizReq struct {
    3. Integer string `v:"integer"`
    4. Float string `v:"integer"`
    5. Str string `v:"integer"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Integer: "100",
    11. Float: "10.0",
    12. Str: "goframe",
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Print(gstr.Join(err.Strings(), "\n"))
    17. }
    18. // Output:
    19. // The Float value `10.0` is not an integer
    20. // The Str value `goframe` is not an integer
    21. }

float

  • 格式: float
  • 说明:浮点数。

    1. func Example_Rule_Float() {
    2. type BizReq struct {
    3. Integer string `v:"float"`
    4. Float string `v:"float"`
    5. Str string `v:"float"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Integer: "100",
    11. Float: "10.0",
    12. Str: "goframe",
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Print(err)
    17. }
    18. // Output:
    19. // The Str value `goframe` is invalid
    20. }

boolean

  • 格式: boolean
  • 说明:布尔值(1,true,on,yestrue | 0,false,off,no,""false)。

    1. func Example_Rule_Boolean() {
    2. type BizReq struct {
    3. Boolean bool `v:"boolean"`
    4. Integer int `v:"boolean"`
    5. Float float32 `v:"boolean"`
    6. Str1 string `v:"boolean"`
    7. Str2 string `v:"boolean"`
    8. Str3 string `v:"boolean"`
    9. }
    10. var (
    11. ctx = context.Background()
    12. req = BizReq{
    13. Boolean: true,
    14. Integer: 1,
    15. Float: 10.0,
    16. Str1: "on",
    17. Str2: "",
    18. Str3: "goframe",
    19. }
    20. )
    21. if err := g.Validator().Data(req).Run(ctx); err != nil {
    22. fmt.Print(gstr.Join(err.Strings(), "\n"))
    23. }
    24. // Output:
    25. // The Float value `10` field must be true or false
    26. // The Str3 value `goframe` field must be true or false
    27. }

same

  • 格式: same:field
  • 说明:参数值必需与field字段参数的值相同。
  • 示例:在用户注册时,提交密码Password和确认密码Password2必须相等(服务端校验)。

    1. func Example_Rule_Same() {
    2. type BizReq struct {
    3. Name string `v:"required"`
    4. Password string `v:"required|same:Password2"`
    5. Password2 string `v:"required"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Name: "gf",
    11. Password: "goframe.org",
    12. Password2: "goframe.net",
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Println(err)
    17. }
    18. // Output:
    19. // The Password value `goframe.org` must be the same as field Password2
    20. }

different

  • 格式: different:field
  • 说明:参数值不能与field字段参数的值相同。
  • 示例:备用邮箱OtherMailAddr和邮箱地址MailAddr必须不相同。

    1. func Example_Rule_Different() {
    2. type BizReq struct {
    3. Name string `v:"required"`
    4. MailAddr string `v:"required"`
    5. ConfirmMailAddr string `v:"required|different:MailAddr"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Name: "gf",
    11. MailAddr: "gf@goframe.org",
    12. ConfirmMailAddr: "gf@goframe.org",
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Println(err)
    17. }
    18. // Output:
    19. // The ConfirmMailAddr value `gf@goframe.org` must be different from field MailAddr
    20. }

eq

  • 格式: eq:field
  • 说明:参数值必需与field字段参数的值相同。same规则的别名,功能同same规则。

  • 版本:框架版本>=v2.2.0

    1. func Example_Rule_EQ() {
    2. type BizReq struct {
    3. Name string `v:"required"`
    4. Password string `v:"required|eq:Password2"`
    5. Password2 string `v:"required"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Name: "gf",
    11. Password: "goframe.org",
    12. Password2: "goframe.net",
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Println(err)
    17. }
    18. // Output:
    19. // The Password value `goframe.org` must be equal to field Password2 value `goframe.net`
    20. }

not-eq

  • 格式: not-eq:field
  • 说明:参数值必需与field字段参数的值不相同。different规则的别名,功能同different规则。

  • 版本:框架版本>=v2.2.0

    1. func Example_Rule_NotEQ() {
    2. type BizReq struct {
    3. Name string `v:"required"`
    4. MailAddr string `v:"required"`
    5. OtherMailAddr string `v:"required|not-eq:MailAddr"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Name: "gf",
    11. MailAddr: "gf@goframe.org",
    12. OtherMailAddr: "gf@goframe.org",
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Println(err)
    17. }
    18. // Output:
    19. // The OtherMailAddr value `gf@goframe.org` must not be equal to field MailAddr value `gf@goframe.org`
    20. }

gt

  • 格式: gt:field
  • 说明:参数值必需大于给定字段对应的值。

  • 版本:框架版本>=v2.2.0

    1. func Example_Rule_GT() {
    2. type BizReq struct {
    3. Value1 int
    4. Value2 int `v:"gt:Value1"`
    5. Value3 int `v:"gt:Value1"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Value1: 1,
    11. Value2: 1,
    12. Value3: 2,
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Println(err.String())
    17. }
    18. // Output:
    19. // The Value2 value `1` must be greater than field Value1 value `1`
    20. }

gte

  • 格式: gte:field
  • 说明:参数值必需大于或等于给定字段对应的值。

  • 版本:框架版本>=v2.2.0

    1. func Example_Rule_GTE() {
    2. type BizReq struct {
    3. Value1 int
    4. Value2 int `v:"gte:Value1"`
    5. Value3 int `v:"gte:Value1"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Value1: 2,
    11. Value2: 1,
    12. Value3: 2,
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Println(err.String())
    17. }
    18. // Output:
    19. // The Value2 value `1` must be greater than or equal to field Value1 value `2`
    20. }

lt

  • 格式: lt:field
  • 说明:参数值必需小于给定字段对应的值。

  • 版本:框架版本>=v2.2.0

    1. func Example_Rule_LT() {
    2. type BizReq struct {
    3. Value1 int
    4. Value2 int `v:"lt:Value1"`
    5. Value3 int `v:"lt:Value1"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Value1: 2,
    11. Value2: 1,
    12. Value3: 2,
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Println(err.String())
    17. }
    18. // Output:
    19. // The Value3 value `2` must be lesser than field Value1 value `2`
    20. }

lte

  • 格式: lte:field
  • 说明:参数值必需小于或等于给定字段对应的值。

  • 版本:框架版本>=v2.2.0

    1. func Example_Rule_LTE() {
    2. type BizReq struct {
    3. Value1 int
    4. Value2 int `v:"lte:Value1"`
    5. Value3 int `v:"lte:Value1"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Value1: 1,
    11. Value2: 1,
    12. Value3: 2,
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Println(err.String())
    17. }
    18. // Output:
    19. // The Value3 value `2` must be lesser than or equal to field Value1 value `1`
    20. }

in

  • 格式: in:value1,value2,...
  • 说明:参数值应该在value1,value2,...中(字符串匹配)。
  • 示例:性别字段Gender的值必须在0/1/2中。

    1. func Example_Rule_In() {
    2. type BizReq struct {
    3. ID uint `v:"required" dc:"Your Id"`
    4. Name string `v:"required" dc:"Your name"`
    5. Gender uint `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. ID: 1,
    11. Name: "test",
    12. Gender: 3,
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Println(err)
    17. }
    18. // Output:
    19. // The Gender value `3` is not in acceptable range: 0,1,2
    20. }

not-in

  • 格式: not-in:value1,value2,...
  • 说明:参数值不应该在value1,value2,...中(字符串匹配)。
  • 示例:无效索引InvalidIndex的值必须不在-1/0/1中。

    1. func Example_Rule_NotIn() {
    2. type BizReq struct {
    3. ID uint `v:"required" dc:"Your Id"`
    4. Name string `v:"required" dc:"Your name"`
    5. InvalidIndex uint `v:"not-in:-1,0,1"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. ID: 1,
    11. Name: "test",
    12. InvalidIndex: 1,
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Println(err)
    17. }
    18. // Output:
    19. // The InvalidIndex value `1` must not be in range: -1,0,1
    20. }

regex

  • 格式: regex:pattern
  • 说明:参数值应当满足正则匹配规则pattern

    1. func Example_Rule_Regex() {
    2. type BizReq struct {
    3. Regex1 string `v:"regex:[1-9][0-9]{4,14}"`
    4. Regex2 string `v:"regex:[1-9][0-9]{4,14}"`
    5. Regex3 string `v:"regex:[1-9][0-9]{4,14}"`
    6. }
    7. var (
    8. ctx = context.Background()
    9. req = BizReq{
    10. Regex1: "1234",
    11. Regex2: "01234",
    12. Regex3: "10000",
    13. }
    14. )
    15. if err := g.Validator().Data(req).Run(ctx); err != nil {
    16. fmt.Print(gstr.Join(err.Strings(), "\n"))
    17. }
    18. // Output:
    19. // The Regex1 value `1234` must be in regex of: [1-9][0-9]{4,14}
    20. // The Regex2 value `01234` must be in regex of: [1-9][0-9]{4,14}
    21. }

not-regex

  • 格式: not-regex:pattern
  • 说明:参数值不应当满足正则匹配规则pattern

  • 版本:框架版本>=v2.2.0

    1. func Example_Rule_NotRegex() {
    2. type BizReq struct {
    3. Regex1 string `v:"regex:\\d{4}"`
    4. Regex2 string `v:"not-regex:\\d{4}"`
    5. }
    6. var (
    7. ctx = context.Background()
    8. req = BizReq{
    9. Regex1: "1234",
    10. Regex2: "1234",
    11. }
    12. )
    13. if err := g.Validator().Data(req).Run(ctx); err != nil {
    14. fmt.Print(gstr.Join(err.Strings(), "\n"))
    15. }
    16. // Output:
    17. // The Regex2 value `1234` should not be in regex of: \d{4}
    18. }