The GoFrame framework’s validation component comes with dozens of common validation rules. The validation component is one of the most frequently used core components by developers.

Data Validation - Rules - 图1info

When the validation rules involve scenarios of joint validation, the parameter name associated in the rules will be automatically intelligently matched in a case-insensitive manner and ignoring special characters. In the following example codes, we use the common struct validation method, where the v tag in the struct property stands for validation abbreviation, used to set the validation specifications for that property.

Modifier Rules

Modifier rules have no validation logic themselves but modify the implementation logic of subsequent functional rules.

ci

  • Format: ci

  • Description: By default, field value comparisons are case-sensitive strict match comparisons. Using the ci(Case Insensitive) modifier rule, you can set the subsequent rule fields requiring comparison to be case-insensitive. Such as: same, different, in, not-in, etc.

  • Example:

    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. }

bail

Special attention is needed: If there are multiple validation rules for parameter validation, and there is a required* rule used together with the bail modifier rule, it is recommended to place the required* validation rule before all other rules. Otherwise, the feature of the bail validation rule (stopping subsequent validations upon failure) may cause the subsequent required* rules not to take effect.

  • Format: bail

  • Description: As soon as one of the multiple subsequent validations fails, it stops validation and immediately returns the validation result.

  • Note: In the framework’s HTTP Server component, if using the standard route registration method, the automatic validation feature will automatically enable the bail modifier rule, and developers do not need to explicitly set bail in the Req object.

  • Example:

    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. }

foreach

  • Format: foreach

  • Description: Used for array parameters, it iterates the parameter as an array and applies the subsequent validation rule to each item in the array.

  • Version: Framework version >=v2.2.0

  • Example:

    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. }

Functional Rules

Functional rules implement specific validation logic, with the framework providing a very rich set of built-in validation rules.

required

  • Format: required

  • Description: Required parameter, supporting not only common strings but also Slice/Map types.

  • Example: The name field Name is a required parameter and must not be empty.

    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

  • Format: required-if:field,value,...

  • Description: Required parameter (when any of the given field values equals the given value, i.e.: when the field‘s value is value, the current validation field is a required parameter). Multiple fields are separated by ,.

  • Example: When the Gender field is 1, the WifeName field must not be empty, and when the Gender field is 2, the HusbandName field must not be empty.

    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-if-all

  • Format: required-if-all:field1,value1,field2,value2,...

  • Description: Required parameter (when all given field values equal the given values, i.e.: when the field1‘s value is value1, and the field2‘s value is value2, and so on, the current validation field is a required parameter). Multiple fields are separated by ,.

  • Example: When the ID field is 1, and the Age field is 18, the MoreInfo field must not be empty.

    1. func ExampleRule_RequiredIfAll() {
    2. type BizReq struct {
    3. ID uint `v:"required" dc:"Your ID"`
    4. Name string `v:"required" dc:"Your name"`
    5. Age int `v:"required" dc:"Your age"`
    6. MoreInfo string `v:"required-if-all:id,1,age,18" dc:"Your more info"`
    7. }
    8. var (
    9. ctx = context.Background()
    10. req = BizReq{
    11. ID: 1,
    12. Name: "test",
    13. Age: 18,
    14. }
    15. )
    16. if err := g.Validator().Data(req).Run(ctx); err != nil {
    17. fmt.Println(err)
    18. }
    19. // Output:
    20. // The MoreInfo field is required
    21. }

required-unless

  • Format: required-unless:field,value,...

  • Description: Required parameter (when the given field values are all not equal to the given values, i.e.: when the field‘s value is not value, the current validation field is a required parameter). Multiple fields are separated by ,.

  • Example: When Gender is not equal to 0 and Gender is not equal to 2, the WifeName must not be empty; when Id is not equal to 0 and Gender is not equal to 2, the HusbandName must not be empty.

    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

  • Format: required-with:field1,field2,...

  • Description: Required parameter (when any of the given field values is not empty).

  • Example: When WifeName is not empty, HusbandName must not be empty.

    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

  • Format: required-with-all:field1,field2,...

  • Description: Required parameter (when all of the given field values are not empty).

  • Example: When Id, Name, Gender, WifeName are all not empty, HusbandName must not be empty.

    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

  • Format: required-without:field1,field2,...

  • Description: Required parameter (when any of the given field values is empty).

  • Example: When Id or WifeName is empty, HusbandName must not be empty.

    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

  • Format: required-without-all:field1,field2,...

  • Description: Required parameter (when all of the given field values are empty).

  • Example: When Id and WifeName are both empty, HusbandName must not be empty.

    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

  • Format: date

  • Description: Parameter is in common date format, supporting separators - or / or ., and also supports 8-digit date without separators, formats like: 2006-01-02, 2006/01/02, 2006.01.02, 20060102

  • Example:

    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

  • Format: datetime

  • Description: Parameter is in common date-time format; only - is supported as a separator between dates, formats like: 2006-01-02 12:00:00

  • Example:

    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

  • Format: date-format:format

  • Description: Checks whether the date is in the specified date/time format, with the format parameter in gtime date format (can include date and time), format description refer to section: Time

  • Example: 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

  • Format: before:field

  • Description: Checks whether the given date/time is before the date/time of the specified field.

  • Version: Framework version >=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

  • Format: before-equal:field

  • Description: Checks whether the given date/time is before or equal to the date/time of the specified field.

  • Version: Framework version >=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

  • Format: after:field

  • Description: Checks whether the given date/time is after the date/time of the specified field.

  • Version: Framework version >=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

  • Format: after-equal:field

  • Description: Checks whether the given date/time is after or equal to the date/time of the specified field.

  • Version: Framework version >=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

  • Format: array

  • Description: Checks whether the given parameter is in array format. If the given parameter is a JSON array string, validation will also pass.

  • Version: Framework version >=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. }

enums

  • Format: enums

  • Description: Validates whether the submitted parameter is within the enum values of the field type. This rule needs to be used in conjunction with the gf gen enums command, for more details refer to: Enums Maintenance

    1. func ExampleRule_Enums() {
    2. type Status string
    3. const (
    4. StatusRunning Status = "Running"
    5. StatusOffline Status = "Offline"
    6. )
    7. type BizReq struct {
    8. Id int `v:"required"`
    9. Name string `v:"required"`
    10. Status Status `v:"enums"`
    11. }
    12. var (
    13. ctx = context.Background()
    14. req = BizReq{
    15. Id: 1,
    16. Name: "john",
    17. Status: Status("Pending"),
    18. }
    19. )
    20. if err := g.Validator().Data(req).Run(ctx); err != nil {
    21. fmt.Print(gstr.Join(err.Strings(), "\n"))
    22. }
    23. // May Output:
    24. // The Status value `Pending` should be in enums of: ["Running","Offline"]
    25. }

email

  • Format: email

  • Description: EMAIL address format.

    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

  • Format: phone

  • Description: Mobile phone number format for China.

    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: "11178912345", // 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 `11178912345` is not a valid phone number
    23. // The PhoneNumber4 value `1357891234` is not a valid phone number
    24. }

phone-loose

  • Format: phone

  • Description: Loose phone number verification, as long as it starts with 13, 14, 15, 16, 17, 18, 19 and is an 11-digit number, verification will pass. This can be used in less strict business scenarios.

    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

  • Format: telephone

  • Description: Landline number in China, formats like “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

  • Format: passport

  • Description: General account rule (starts with a letter, can only contain letters, numbers, and underscores, with a length between 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

  • Format: password

  • Description: General password rule (any visible character, length between 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

  • Format: password2

  • Description: Medium strength password (on the basis of general password rule, it must contain uppercase and lowercase letters and numbers).

    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

  • Format: password3

  • Description: Strong strength password (on the basis of general password rule, it must contain uppercase and lowercase letters, numbers, and special characters).

    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

  • Format: postcode

  • Description: Postal code rule for China.

    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

  • Format: resident-id

  • Description: Resident ID card number.

    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

  • Format: bank-card

  • Description: Bank card number validation for China.

    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

  • Format: qq

  • Description: Tencent QQ number rule.

    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

  • Format: ip

  • Description: IPv4/IPv6 address.

    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

  • Format: ipv4

  • Description: IPv4 address.

    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

  • Format: ipv6

  • Description: IPv6 address.

    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

  • Format: mac

  • Description: MAC address.

    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

  • Format: url

  • Description: URL

  • Example: Supports addresses starting with 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

  • Format: domain

  • Description: Domain

  • Example: Domain rule. xxx.yyy (must start with a letter).

    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

  • Format: size:size

  • Description: The parameter length is size (length parameter is integer), note that Unicode is used for length calculation at the underlying layer, thus a Chinese character takes up 1 length unit.

    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

  • Format: length:min,max

  • Description: The parameter length is between min and max (length parameter is integer), note that Unicode is used for length calculation at the underlying layer, thus a Chinese character takes up 1 length unit.

    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

  • Format: min-length:min

  • Description: The parameter length minimum is min (length parameter is integer), note that Unicode is used for length calculation at the underlying layer, thus a Chinese character takes up 1 length unit.

    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

  • Format: max-length:max

  • Description: The parameter length maximum is max (length parameter is integer), note that Unicode is used for length calculation at the underlying layer, thus a Chinese character takes up 1 length unit.

    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

  • Format: between:min,max

  • Description: The parameter size is between min and max (supports integer and float type parameters).

    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,10"`
    6. Score2 float32 `v:"between:0,10"`
    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

  • Format: min:min

  • Description: The parameter size minimum is min (supports integer and float type parameters).

    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"`
    6. Score2 float32 `v:"min:10"`
    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

  • Format: max:max

  • Description: The parameter size maximum is max (supports integer and float type parameters).

    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"`
    6. Score2 float32 `v:"max:10"`
    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

  • Format: json

  • Description: Checks whether the data format is 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

  • Format: integer

  • Description: Integer (positive or negative).

    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

  • Format: float

  • Description: Float numbers.

    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

  • Format: boolean

  • Description: Boolean value (1, true, on, yes for true | 0, false, off, no, "" for 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

  • Format: same:field

  • Description: The parameter value must be the same as the value of the field parameter.

  • Example: During user registration, the submitted password Password and confirm password Password2 must be equal (server-side validation).

    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

  • Format: different:field

  • Description: Parameter value cannot be the same as the field parameter value.

  • Example: Backup email OtherMailAddr and email address MailAddr must be different.

    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

  • Format: eq:field

  • Description: Parameter value must be the same as the value of the field parameter. Alias for same rule, identical to same rule.

  • Version: Framework version >=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

  • Format: not-eq:field

  • Description: Parameter value must be different from the field’s parameter value. Alias for different rule, identical to different rule.

  • Version: Framework version >=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

  • Format: gt:field

  • Description: Parameter value must be greater than the value corresponding to the given field.

  • Version: Framework version >=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

  • Format: gte:field

  • Description: Parameter value must be greater than or equal to the value corresponding to the given field.

  • Version: Framework version >=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

  • Format: lt:field

  • Description: Parameter value must be less than the value corresponding to the given field.

  • Version: Framework version >=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

  • Format: lte:field

  • Description: Parameter value must be less than or equal to the value corresponding to the given field.

  • Version: Framework version >=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

  • Format: in:value1,value2,...

  • Description: Parameter value should be within value1,value2,... (string matching).

  • Example: The value of the gender field Gender must be within 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

  • Format: not-in:value1,value2,...

  • Description: Parameter value should not be within value1,value2,... (string matching).

  • Example: The invalid index InvalidIndex must not be within -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

  • Format: regex:pattern

  • Description: The parameter value should meet the regex matching rule 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

  • Format: not-regex:pattern

  • Description: The parameter value should not meet the regex matching rule pattern.

  • Version: Framework version >=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. }