github.com/wangyougui/gf/v2@v2.6.5/util/gvalid/gvalid_z_unit_feature_i18n_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gvalid_test
     8  
     9  import (
    10  	"context"
    11  	"testing"
    12  
    13  	"github.com/wangyougui/gf/v2/i18n/gi18n"
    14  	"github.com/wangyougui/gf/v2/test/gtest"
    15  	"github.com/wangyougui/gf/v2/util/gvalid"
    16  )
    17  
    18  func TestValidator_I18n(t *testing.T) {
    19  	var (
    20  		err         gvalid.Error
    21  		i18nManager = gi18n.New(gi18n.Options{Path: gtest.DataPath("i18n")})
    22  		ctxCn       = gi18n.WithLanguage(context.TODO(), "cn")
    23  		validator   = gvalid.New().I18n(i18nManager)
    24  	)
    25  	gtest.C(t, func(t *gtest.T) {
    26  		err = validator.Rules("required").Data("").Run(ctx)
    27  		t.Assert(err.String(), "The field is required")
    28  
    29  		err = validator.Rules("required").Data("").Run(ctxCn)
    30  		t.Assert(err.String(), "字段不能为空")
    31  	})
    32  	gtest.C(t, func(t *gtest.T) {
    33  		err = validator.Rules("required").Messages("CustomMessage").Data("").Run(ctxCn)
    34  		t.Assert(err.String(), "自定义错误")
    35  	})
    36  	gtest.C(t, func(t *gtest.T) {
    37  		type Params struct {
    38  			Page      int `v:"required|min:1         # page is required"`
    39  			Size      int `v:"required|between:1,100 # size is required"`
    40  			ProjectId int `v:"between:1,10000        # project id must between {min}, {max}"`
    41  		}
    42  		obj := &Params{
    43  			Page: 1,
    44  			Size: 10,
    45  		}
    46  		err = validator.Data(obj).Run(ctxCn)
    47  		t.Assert(err.String(), "项目ID必须大于等于1并且要小于等于10000")
    48  	})
    49  }