@@ -13351,6 +13351,100 @@ func TestValidate_ValidateMapCtx(t *testing.T) {
1335113351 }
1335213352}
1335313353
13354+ func TestValidate_ValidateMapCtxWithKeys (t * testing.T ) {
13355+ type args struct {
13356+ data map [string ]interface {}
13357+ rules map [string ]interface {}
13358+ errors map [string ]interface {}
13359+ }
13360+ tests := []struct {
13361+ name string
13362+ args args
13363+ want int
13364+ }{
13365+ {
13366+ name : "test invalid email" ,
13367+ args : args {
13368+ data : map [string ]interface {}{
13369+ "email" : "emailaddress" ,
13370+ },
13371+ rules : map [string ]interface {}{
13372+ "email" : "required,email" ,
13373+ },
13374+ errors : map [string ]interface {}{
13375+ "email" : "Key: 'email' Error:Field validation for 'email' failed on the 'email' tag" ,
13376+ },
13377+ },
13378+ want : 1 ,
13379+ },
13380+ {
13381+ name : "test multiple errors with capitalized keys" ,
13382+ args : args {
13383+ data : map [string ]interface {}{
13384+ "Email" : "emailaddress" ,
13385+ "Age" : 15 ,
13386+ },
13387+ rules : map [string ]interface {}{
13388+ "Email" : "required,email" ,
13389+ "Age" : "number,gt=16" ,
13390+ },
13391+ errors : map [string ]interface {}{
13392+ "Email" : "Key: 'Email' Error:Field validation for 'Email' failed on the 'email' tag" ,
13393+ "Age" : "Key: 'Age' Error:Field validation for 'Age' failed on the 'gt' tag" ,
13394+ },
13395+ },
13396+ want : 2 ,
13397+ },
13398+ {
13399+ name : "test valid map data" ,
13400+ args : args {
13401+ data : map [string ]interface {}{
13402+ "email" : "email@example.com" ,
13403+ "age" : 17 ,
13404+ },
13405+ rules : map [string ]interface {}{
13406+ "email" : "required,email" ,
13407+ "age" : "number,gt=16" ,
13408+ },
13409+ errors : map [string ]interface {}{},
13410+ },
13411+ want : 0 ,
13412+ },
13413+ }
13414+
13415+ for _ , tt := range tests {
13416+ t .Run (tt .name , func (t * testing.T ) {
13417+ validate := New ()
13418+ errs := validate .ValidateMapCtx (context .Background (), tt .args .data , tt .args .rules )
13419+ NotEqual (t , errs , nil )
13420+ Equal (t , len (errs ), tt .want )
13421+ for key , err := range errs {
13422+ Equal (t , err .(ValidationErrors )[0 ].Error (), tt .args .errors [key ])
13423+ }
13424+ })
13425+ }
13426+ }
13427+
13428+ func TestValidate_VarWithKey (t * testing.T ) {
13429+ validate := New ()
13430+ errs := validate .VarWithKey ("email" , "invalidemail" , "required,email" )
13431+ NotEqual (t , errs , nil )
13432+ AssertError (t , errs , "email" , "email" , "email" , "email" , "email" )
13433+
13434+ errs = validate .VarWithKey ("email" , "email@example.com" , "required,email" )
13435+ Equal (t , errs , nil )
13436+ }
13437+
13438+ func TestValidate_VarWithKeyCtx (t * testing.T ) {
13439+ validate := New ()
13440+ errs := validate .VarWithKeyCtx (context .Background (), "age" , 15 , "required,gt=16" )
13441+ NotEqual (t , errs , nil )
13442+ AssertError (t , errs , "age" , "age" , "age" , "age" , "gt" )
13443+
13444+ errs = validate .VarWithKey ("age" , 17 , "required,gt=16" )
13445+ Equal (t , errs , nil )
13446+ }
13447+
1335413448func TestMongoDBObjectIDFormatValidation (t * testing.T ) {
1335513449 tests := []struct {
1335613450 value string `validate:"mongodb"`
0 commit comments