discriminate map validate and map array validate

local
hzy 2 years ago
parent f5e5146b31
commit 0e6a5850b2
  1. 2
      _examples/custom-validation/main.go
  2. 2
      _examples/custom/main.go
  3. 2
      _examples/dive/main.go
  4. 2
      _examples/gin-upgrading-overriding/v8_to_v9.go
  5. 2
      _examples/map-validation/main.go
  6. 2
      _examples/simple/main.go
  7. 2
      _examples/struct-level/main.go
  8. 2
      _examples/struct-map-rules-validation/main.go
  9. 4
      _examples/translations/main.go
  10. 2
      go.mod
  11. 2
      non-standard/validators/notblank.go
  12. 2
      non-standard/validators/notblank_test.go
  13. 2
      translations/ar/ar.go
  14. 2
      translations/ar/ar_test.go
  15. 2
      translations/en/en.go
  16. 2
      translations/en/en_test.go
  17. 2
      translations/es/es.go
  18. 2
      translations/es/es_test.go
  19. 2
      translations/fa/fa.go
  20. 2
      translations/fa/fa_test.go
  21. 2
      translations/fr/fr.go
  22. 2
      translations/fr/fr_test.go
  23. 2
      translations/id/id.go
  24. 2
      translations/id/id_test.go
  25. 2
      translations/it/it.go
  26. 2
      translations/it/it_test.go
  27. 1
      translations/ja/ja.go
  28. 2
      translations/ja/ja_test.go
  29. 2
      translations/lv/lv.go
  30. 2
      translations/lv/lv_test.go
  31. 1
      translations/nl/nl.go
  32. 2
      translations/nl/nl_test.go
  33. 1
      translations/pt/pt.go
  34. 3
      translations/pt/pt_test.go
  35. 2
      translations/pt_BR/pt_BR.go
  36. 2
      translations/pt_BR/pt_BR_test.go
  37. 2
      translations/ru/ru.go
  38. 2
      translations/ru/ru_test.go
  39. 2
      translations/tr/tr.go
  40. 2
      translations/tr/tr_test.go
  41. 1
      translations/vi/vi.go
  42. 2
      translations/vi/vi_test.go
  43. 2
      translations/zh/zh.go
  44. 2
      translations/zh/zh_test.go
  45. 1
      translations/zh_tw/zh_tw.go
  46. 2
      translations/zh_tw/zh_tw_test.go
  47. 10
      validator_instance.go

@ -3,7 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/go-playground/validator/v10" "gitea.drugeyes.vip/pharnexbase/validator/v10"
) )
// MyStruct .. // MyStruct ..

@ -6,7 +6,7 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"github.com/go-playground/validator/v10" "gitea.drugeyes.vip/pharnexbase/validator/v10"
) )
// DbBackedUser User struct // DbBackedUser User struct

@ -3,7 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/go-playground/validator/v10" "gitea.drugeyes.vip/pharnexbase/validator/v10"
) )
// Test ... // Test ...

@ -4,8 +4,8 @@ import (
"reflect" "reflect"
"sync" "sync"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"github.com/gin-gonic/gin/binding" "github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
) )
type defaultValidator struct { type defaultValidator struct {

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/go-playground/validator/v10" "gitea.drugeyes.vip/pharnexbase/validator/v10"
) )
var validate *validator.Validate var validate *validator.Validate

@ -3,7 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/go-playground/validator/v10" "gitea.drugeyes.vip/pharnexbase/validator/v10"
) )
// User contains user information // User contains user information

@ -6,7 +6,7 @@ import (
"reflect" "reflect"
"strings" "strings"
"github.com/go-playground/validator/v10" "gitea.drugeyes.vip/pharnexbase/validator/v10"
) )
type validationError struct { type validationError struct {

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/go-playground/validator/v10" "gitea.drugeyes.vip/pharnexbase/validator/v10"
) )
type Data struct { type Data struct {

@ -3,10 +3,10 @@ package main
import ( import (
"fmt" "fmt"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
en_translations "gitea.drugeyes.vip/pharnexbase/validator/v10/translations/en"
"github.com/go-playground/locales/en" "github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
en_translations "github.com/go-playground/validator/v10/translations/en"
) )
// User contains user information // User contains user information

@ -1,4 +1,4 @@
module github.com/go-playground/validator/v10 module gitea.drugeyes.vip/pharnexbase/validator/v10
go 1.18 go 1.18

@ -4,7 +4,7 @@ import (
"reflect" "reflect"
"strings" "strings"
"github.com/go-playground/validator/v10" "gitea.drugeyes.vip/pharnexbase/validator/v10"
) )
// NotBlank is the validation function for validating if the current field // NotBlank is the validation function for validating if the current field

@ -3,8 +3,8 @@ package validators
import ( import (
"testing" "testing"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"github.com/go-playground/assert/v2" "github.com/go-playground/assert/v2"
"github.com/go-playground/validator/v10"
) )
type test struct { type test struct {

@ -8,9 +8,9 @@ import (
"strings" "strings"
"time" "time"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"github.com/go-playground/locales" "github.com/go-playground/locales"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations

@ -4,10 +4,10 @@ import (
"testing" "testing"
"time" "time"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
. "github.com/go-playground/assert/v2" . "github.com/go-playground/assert/v2"
english "github.com/go-playground/locales/en" english "github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
func TestTranslations(t *testing.T) { func TestTranslations(t *testing.T) {

@ -8,9 +8,9 @@ import (
"strings" "strings"
"time" "time"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"github.com/go-playground/locales" "github.com/go-playground/locales"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations

@ -4,10 +4,10 @@ import (
"testing" "testing"
"time" "time"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
. "github.com/go-playground/assert/v2" . "github.com/go-playground/assert/v2"
english "github.com/go-playground/locales/en" english "github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
func TestTranslations(t *testing.T) { func TestTranslations(t *testing.T) {

@ -8,9 +8,9 @@ import (
"strings" "strings"
"time" "time"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"github.com/go-playground/locales" "github.com/go-playground/locales"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations

@ -4,10 +4,10 @@ import (
"testing" "testing"
"time" "time"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
. "github.com/go-playground/assert/v2" . "github.com/go-playground/assert/v2"
spanish "github.com/go-playground/locales/es" spanish "github.com/go-playground/locales/es"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
func TestTranslations(t *testing.T) { func TestTranslations(t *testing.T) {

@ -8,9 +8,9 @@ import (
"strings" "strings"
"time" "time"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"github.com/go-playground/locales" "github.com/go-playground/locales"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations

@ -4,10 +4,10 @@ import (
"testing" "testing"
"time" "time"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
. "github.com/go-playground/assert/v2" . "github.com/go-playground/assert/v2"
english "github.com/go-playground/locales/en" english "github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
func TestTranslations(t *testing.T) { func TestTranslations(t *testing.T) {

@ -8,9 +8,9 @@ import (
"strings" "strings"
"time" "time"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"github.com/go-playground/locales" "github.com/go-playground/locales"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations

@ -4,10 +4,10 @@ import (
"testing" "testing"
"time" "time"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
. "github.com/go-playground/assert/v2" . "github.com/go-playground/assert/v2"
french "github.com/go-playground/locales/fr" french "github.com/go-playground/locales/fr"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
func TestTranslations(t *testing.T) { func TestTranslations(t *testing.T) {

@ -8,9 +8,9 @@ import (
"strings" "strings"
"time" "time"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"github.com/go-playground/locales" "github.com/go-playground/locales"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations

@ -4,10 +4,10 @@ import (
"testing" "testing"
"time" "time"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
. "github.com/go-playground/assert/v2" . "github.com/go-playground/assert/v2"
indonesia "github.com/go-playground/locales/id" indonesia "github.com/go-playground/locales/id"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
func TestTranslations(t *testing.T) { func TestTranslations(t *testing.T) {

@ -2,6 +2,7 @@ package it
import ( import (
"fmt" "fmt"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"log" "log"
"reflect" "reflect"
"strconv" "strconv"
@ -10,7 +11,6 @@ import (
"github.com/go-playground/locales" "github.com/go-playground/locales"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations

@ -1,13 +1,13 @@
package it package it
import ( import (
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"testing" "testing"
"time" "time"
. "github.com/go-playground/assert/v2" . "github.com/go-playground/assert/v2"
italian "github.com/go-playground/locales/it" italian "github.com/go-playground/locales/it"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
func TestTranslations(t *testing.T) { func TestTranslations(t *testing.T) {

@ -10,7 +10,6 @@ import (
"github.com/go-playground/locales" "github.com/go-playground/locales"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations

@ -1,13 +1,13 @@
package ja package ja
import ( import (
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"testing" "testing"
"time" "time"
. "github.com/go-playground/assert/v2" . "github.com/go-playground/assert/v2"
ja_locale "github.com/go-playground/locales/ja" ja_locale "github.com/go-playground/locales/ja"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
func TestTranslations(t *testing.T) { func TestTranslations(t *testing.T) {

@ -2,6 +2,7 @@ package lv
import ( import (
"fmt" "fmt"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"log" "log"
"reflect" "reflect"
"strconv" "strconv"
@ -10,7 +11,6 @@ import (
"github.com/go-playground/locales" "github.com/go-playground/locales"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations

@ -1,13 +1,13 @@
package lv package lv
import ( import (
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"testing" "testing"
"time" "time"
. "github.com/go-playground/assert/v2" . "github.com/go-playground/assert/v2"
english "github.com/go-playground/locales/en" english "github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
func TestTranslations(t *testing.T) { func TestTranslations(t *testing.T) {

@ -10,7 +10,6 @@ import (
"github.com/go-playground/locales" "github.com/go-playground/locales"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations

@ -1,13 +1,13 @@
package nl package nl
import ( import (
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"testing" "testing"
"time" "time"
. "github.com/go-playground/assert/v2" . "github.com/go-playground/assert/v2"
english "github.com/go-playground/locales/en" english "github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
func TestTranslations(t *testing.T) { func TestTranslations(t *testing.T) {

@ -10,7 +10,6 @@ import (
"github.com/go-playground/locales" "github.com/go-playground/locales"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations

@ -1,14 +1,13 @@
package pt package pt
import ( import (
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"testing" "testing"
"time" "time"
. "github.com/go-playground/assert/v2" . "github.com/go-playground/assert/v2"
"github.com/go-playground/locales/pt" "github.com/go-playground/locales/pt"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
func TestTranslations(t *testing.T) { func TestTranslations(t *testing.T) {

@ -2,6 +2,7 @@ package pt_BR
import ( import (
"fmt" "fmt"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"log" "log"
"reflect" "reflect"
"strconv" "strconv"
@ -10,7 +11,6 @@ import (
"github.com/go-playground/locales" "github.com/go-playground/locales"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations

@ -1,13 +1,13 @@
package pt_BR package pt_BR
import ( import (
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"testing" "testing"
"time" "time"
. "github.com/go-playground/assert/v2" . "github.com/go-playground/assert/v2"
brazilian_portuguese "github.com/go-playground/locales/pt_BR" brazilian_portuguese "github.com/go-playground/locales/pt_BR"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
func TestTranslations(t *testing.T) { func TestTranslations(t *testing.T) {

@ -2,6 +2,7 @@ package ru
import ( import (
"fmt" "fmt"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"log" "log"
"reflect" "reflect"
"strconv" "strconv"
@ -10,7 +11,6 @@ import (
"github.com/go-playground/locales" "github.com/go-playground/locales"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations

@ -1,6 +1,7 @@
package ru package ru
import ( import (
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"log" "log"
//"github.com/rustery/validator" //"github.com/rustery/validator"
"testing" "testing"
@ -9,7 +10,6 @@ import (
. "github.com/go-playground/assert/v2" . "github.com/go-playground/assert/v2"
russian "github.com/go-playground/locales/ru" russian "github.com/go-playground/locales/ru"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
func TestTranslations(t *testing.T) { func TestTranslations(t *testing.T) {

@ -2,6 +2,7 @@ package tr
import ( import (
"fmt" "fmt"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"log" "log"
"reflect" "reflect"
"strconv" "strconv"
@ -10,7 +11,6 @@ import (
"github.com/go-playground/locales" "github.com/go-playground/locales"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations

@ -1,13 +1,13 @@
package tr package tr
import ( import (
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"testing" "testing"
"time" "time"
. "github.com/go-playground/assert/v2" . "github.com/go-playground/assert/v2"
turkish "github.com/go-playground/locales/tr" turkish "github.com/go-playground/locales/tr"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
func TestTranslations(t *testing.T) { func TestTranslations(t *testing.T) {

@ -10,7 +10,6 @@ import (
"github.com/go-playground/locales" "github.com/go-playground/locales"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations

@ -1,13 +1,13 @@
package vi package vi
import ( import (
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"testing" "testing"
"time" "time"
. "github.com/go-playground/assert/v2" . "github.com/go-playground/assert/v2"
vietnamese "github.com/go-playground/locales/vi" vietnamese "github.com/go-playground/locales/vi"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
func TestTranslations(t *testing.T) { func TestTranslations(t *testing.T) {

@ -2,6 +2,7 @@ package zh
import ( import (
"fmt" "fmt"
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"log" "log"
"reflect" "reflect"
"strconv" "strconv"
@ -10,7 +11,6 @@ import (
"github.com/go-playground/locales" "github.com/go-playground/locales"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations

@ -1,13 +1,13 @@
package zh package zh
import ( import (
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"testing" "testing"
"time" "time"
. "github.com/go-playground/assert/v2" . "github.com/go-playground/assert/v2"
zhongwen "github.com/go-playground/locales/zh" zhongwen "github.com/go-playground/locales/zh"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
func TestTranslations(t *testing.T) { func TestTranslations(t *testing.T) {

@ -10,7 +10,6 @@ import (
"github.com/go-playground/locales" "github.com/go-playground/locales"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations

@ -1,13 +1,13 @@
package zh_tw package zh_tw
import ( import (
"gitea.drugeyes.vip/pharnexbase/validator/v10"
"testing" "testing"
"time" "time"
. "github.com/go-playground/assert/v2" . "github.com/go-playground/assert/v2"
zhongwen "github.com/go-playground/locales/zh_Hant_TW" zhongwen "github.com/go-playground/locales/zh_Hant_TW"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
) )
func TestTranslations(t *testing.T) { func TestTranslations(t *testing.T) {

@ -161,15 +161,19 @@ func (v Validate) ValidateMapCtx(ctx context.Context, data map[string]interface{
if len(err) > 0 { if len(err) > 0 {
errs[field] = err errs[field] = err
} }
} else if dataObjs, ok := data[field].([]map[string]interface{}); ok { } else {
errs[field] = errors.New("The field: '" + field + "' is not a map to dive")
}
} else if arrRuleObj, ok := rule.([]map[string]interface{}); ok {
if dataObjs, ok := data[field].([]map[string]interface{}); ok && len(arrRuleObj) > 0 {
for _, obj := range dataObjs { for _, obj := range dataObjs {
err := v.ValidateMapCtx(ctx, obj, ruleObj) err := v.ValidateMapCtx(ctx, obj, arrRuleObj[0])
if len(err) > 0 { if len(err) > 0 {
errs[field] = err errs[field] = err
} }
} }
} else { } else {
errs[field] = errors.New("The field: '" + field + "' is not a map to dive") errs[field] = errors.New("The field: '" + field + "' is not a map array")
} }
} else if ruleStr, ok := rule.(string); ok { } else if ruleStr, ok := rule.(string); ok {
err := v.VarCtx(ctx, data[field], ruleStr) err := v.VarCtx(ctx, data[field], ruleStr)

Loading…
Cancel
Save