update linter and make fixes

updates-and-fixes
Dean Karn 5 years ago
parent 556b9da3c0
commit 8e998f7f22
  1. 8
      Makefile
  2. 215
      benchmarks_test.go
  3. 12
      cache.go
  4. 2
      non-standard/validators/notblank.go
  5. 2
      non-standard/validators/notblank_test.go
  6. 1
      translations/ja/ja.go
  7. 14
      translations/zh/zh.go
  8. 3
      translations/zh/zh_test.go
  9. 14
      translations/zh_tw/zh_tw.go
  10. 6
      translations/zh_tw/zh_tw_test.go
  11. 118
      validator_test.go

@ -1,11 +1,13 @@
GOCMD=go
linters-install:
$(GOCMD) get -u github.com/alecthomas/gometalinter
gometalinter --install
@golangci-lint --version >/dev/null 2>&1 || { \
echo "installing linting tools..."; \
$(GOCMD) get github.com/golangci/golangci-lint/cmd/golangci-lint; \
}
lint: linters-install
gometalinter --vendor --disable-all --enable=vet --enable=vetshadow --enable=golint --enable=maligned --enable=megacheck --enable=ineffassign --enable=misspell --enable=errcheck --enable=goconst ./...
golangci-lint run
test:
$(GOCMD) test -cover -race ./...

@ -8,236 +8,197 @@ import (
)
func BenchmarkFieldSuccess(b *testing.B) {
validate := New()
s := "1"
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Var(&s, "len=1")
_ = validate.Var(&s, "len=1")
}
}
func BenchmarkFieldSuccessParallel(b *testing.B) {
validate := New()
s := "1"
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Var(&s, "len=1")
_ = validate.Var(&s, "len=1")
}
})
}
func BenchmarkFieldFailure(b *testing.B) {
validate := New()
s := "12"
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Var(&s, "len=1")
_ = validate.Var(&s, "len=1")
}
}
func BenchmarkFieldFailureParallel(b *testing.B) {
validate := New()
s := "12"
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Var(&s, "len=1")
_ = validate.Var(&s, "len=1")
}
})
}
func BenchmarkFieldArrayDiveSuccess(b *testing.B) {
validate := New()
m := []string{"val1", "val2", "val3"}
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Var(m, "required,dive,required")
_ = validate.Var(m, "required,dive,required")
}
}
func BenchmarkFieldArrayDiveSuccessParallel(b *testing.B) {
validate := New()
m := []string{"val1", "val2", "val3"}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Var(m, "required,dive,required")
_ = validate.Var(m, "required,dive,required")
}
})
}
func BenchmarkFieldArrayDiveFailure(b *testing.B) {
validate := New()
m := []string{"val1", "", "val3"}
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Var(m, "required,dive,required")
_ = validate.Var(m, "required,dive,required")
}
}
func BenchmarkFieldArrayDiveFailureParallel(b *testing.B) {
validate := New()
m := []string{"val1", "", "val3"}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Var(m, "required,dive,required")
_ = validate.Var(m, "required,dive,required")
}
})
}
func BenchmarkFieldMapDiveSuccess(b *testing.B) {
validate := New()
m := map[string]string{"val1": "val1", "val2": "val2", "val3": "val3"}
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Var(m, "required,dive,required")
_ = validate.Var(m, "required,dive,required")
}
}
func BenchmarkFieldMapDiveSuccessParallel(b *testing.B) {
validate := New()
m := map[string]string{"val1": "val1", "val2": "val2", "val3": "val3"}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Var(m, "required,dive,required")
_ = validate.Var(m, "required,dive,required")
}
})
}
func BenchmarkFieldMapDiveFailure(b *testing.B) {
validate := New()
m := map[string]string{"": "", "val3": "val3"}
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Var(m, "required,dive,required")
_ = validate.Var(m, "required,dive,required")
}
}
func BenchmarkFieldMapDiveFailureParallel(b *testing.B) {
validate := New()
m := map[string]string{"": "", "val3": "val3"}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Var(m, "required,dive,required")
_ = validate.Var(m, "required,dive,required")
}
})
}
func BenchmarkFieldMapDiveWithKeysSuccess(b *testing.B) {
validate := New()
m := map[string]string{"val1": "val1", "val2": "val2", "val3": "val3"}
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Var(m, "required,dive,keys,required,endkeys,required")
_ = validate.Var(m, "required,dive,keys,required,endkeys,required")
}
}
func BenchmarkFieldMapDiveWithKeysSuccessParallel(b *testing.B) {
validate := New()
m := map[string]string{"val1": "val1", "val2": "val2", "val3": "val3"}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Var(m, "required,dive,keys,required,endkeys,required")
_ = validate.Var(m, "required,dive,keys,required,endkeys,required")
}
})
}
func BenchmarkFieldMapDiveWithKeysFailure(b *testing.B) {
validate := New()
m := map[string]string{"": "", "val3": "val3"}
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Var(m, "required,dive,keys,required,endkeys,required")
_ = validate.Var(m, "required,dive,keys,required,endkeys,required")
}
}
func BenchmarkFieldMapDiveWithKeysFailureParallel(b *testing.B) {
validate := New()
m := map[string]string{"": "", "val3": "val3"}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Var(m, "required,dive,keys,required,endkeys,required")
_ = validate.Var(m, "required,dive,keys,required,endkeys,required")
}
})
}
func BenchmarkFieldCustomTypeSuccess(b *testing.B) {
validate := New()
validate.RegisterCustomTypeFunc(ValidateValuerType, (*sql.Valuer)(nil), valuer{})
val := valuer{
Name: "1",
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Var(val, "len=1")
_ = validate.Var(val, "len=1")
}
}
func BenchmarkFieldCustomTypeSuccessParallel(b *testing.B) {
validate := New()
validate.RegisterCustomTypeFunc(ValidateValuerType, (*sql.Valuer)(nil), valuer{})
val := valuer{
Name: "1",
}
@ -245,111 +206,95 @@ func BenchmarkFieldCustomTypeSuccessParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Var(val, "len=1")
_ = validate.Var(val, "len=1")
}
})
}
func BenchmarkFieldCustomTypeFailure(b *testing.B) {
validate := New()
validate.RegisterCustomTypeFunc(ValidateValuerType, (*sql.Valuer)(nil), valuer{})
val := valuer{}
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Var(val, "len=1")
_ = validate.Var(val, "len=1")
}
}
func BenchmarkFieldCustomTypeFailureParallel(b *testing.B) {
validate := New()
validate.RegisterCustomTypeFunc(ValidateValuerType, (*sql.Valuer)(nil), valuer{})
val := valuer{}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Var(val, "len=1")
_ = validate.Var(val, "len=1")
}
})
}
func BenchmarkFieldOrTagSuccess(b *testing.B) {
validate := New()
s := "rgba(0,0,0,1)"
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Var(s, "rgb|rgba")
_ = validate.Var(s, "rgb|rgba")
}
}
func BenchmarkFieldOrTagSuccessParallel(b *testing.B) {
validate := New()
s := "rgba(0,0,0,1)"
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Var(s, "rgb|rgba")
_ = validate.Var(s, "rgb|rgba")
}
})
}
func BenchmarkFieldOrTagFailure(b *testing.B) {
validate := New()
s := "#000"
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Var(s, "rgb|rgba")
_ = validate.Var(s, "rgb|rgba")
}
}
func BenchmarkFieldOrTagFailureParallel(b *testing.B) {
validate := New()
s := "#000"
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Var(s, "rgb|rgba")
_ = validate.Var(s, "rgb|rgba")
}
})
}
func BenchmarkStructLevelValidationSuccess(b *testing.B) {
validate := New()
validate.RegisterStructValidation(StructValidationTestStructSuccess, TestStruct{})
tst := TestStruct{
String: "good value",
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Struct(tst)
_ = validate.Struct(tst)
}
}
func BenchmarkStructLevelValidationSuccessParallel(b *testing.B) {
validate := New()
validate.RegisterStructValidation(StructValidationTestStructSuccess, TestStruct{})
tst := TestStruct{
String: "good value",
}
@ -357,31 +302,27 @@ func BenchmarkStructLevelValidationSuccessParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Struct(tst)
_ = validate.Struct(tst)
}
})
}
func BenchmarkStructLevelValidationFailure(b *testing.B) {
validate := New()
validate.RegisterStructValidation(StructValidationTestStruct, TestStruct{})
tst := TestStruct{
String: "good value",
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Struct(tst)
_ = validate.Struct(tst)
}
}
func BenchmarkStructLevelValidationFailureParallel(b *testing.B) {
validate := New()
validate.RegisterStructValidation(StructValidationTestStruct, TestStruct{})
tst := TestStruct{
String: "good value",
}
@ -389,13 +330,12 @@ func BenchmarkStructLevelValidationFailureParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Struct(tst)
_ = validate.Struct(tst)
}
})
}
func BenchmarkStructSimpleCustomTypeSuccess(b *testing.B) {
validate := New()
validate.RegisterCustomTypeFunc(ValidateValuerType, (*sql.Valuer)(nil), valuer{})
@ -412,12 +352,11 @@ func BenchmarkStructSimpleCustomTypeSuccess(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Struct(validFoo)
_ = validate.Struct(validFoo)
}
}
func BenchmarkStructSimpleCustomTypeSuccessParallel(b *testing.B) {
validate := New()
validate.RegisterCustomTypeFunc(ValidateValuerType, (*sql.Valuer)(nil), valuer{})
@ -435,13 +374,12 @@ func BenchmarkStructSimpleCustomTypeSuccessParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Struct(validFoo)
_ = validate.Struct(validFoo)
}
})
}
func BenchmarkStructSimpleCustomTypeFailure(b *testing.B) {
validate := New()
validate.RegisterCustomTypeFunc(ValidateValuerType, (*sql.Valuer)(nil), valuer{})
@ -456,12 +394,11 @@ func BenchmarkStructSimpleCustomTypeFailure(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Struct(validFoo)
_ = validate.Struct(validFoo)
}
}
func BenchmarkStructSimpleCustomTypeFailureParallel(b *testing.B) {
validate := New()
validate.RegisterCustomTypeFunc(ValidateValuerType, (*sql.Valuer)(nil), valuer{})
@ -477,13 +414,12 @@ func BenchmarkStructSimpleCustomTypeFailureParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Struct(validate.Struct(validFoo))
_ = validate.Struct(validate.Struct(validFoo))
}
})
}
func BenchmarkStructFilteredSuccess(b *testing.B) {
validate := New()
type Test struct {
@ -503,12 +439,11 @@ func BenchmarkStructFilteredSuccess(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.StructFiltered(test, fn)
_ = validate.StructFiltered(test, fn)
}
}
func BenchmarkStructFilteredSuccessParallel(b *testing.B) {
validate := New()
type Test struct {
@ -529,13 +464,12 @@ func BenchmarkStructFilteredSuccessParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.StructFiltered(test, fn)
_ = validate.StructFiltered(test, fn)
}
})
}
func BenchmarkStructFilteredFailure(b *testing.B) {
validate := New()
type Test struct {
@ -555,12 +489,11 @@ func BenchmarkStructFilteredFailure(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.StructFiltered(test, fn)
_ = validate.StructFiltered(test, fn)
}
}
func BenchmarkStructFilteredFailureParallel(b *testing.B) {
validate := New()
type Test struct {
@ -581,13 +514,12 @@ func BenchmarkStructFilteredFailureParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.StructFiltered(test, fn)
_ = validate.StructFiltered(test, fn)
}
})
}
func BenchmarkStructPartialSuccess(b *testing.B) {
validate := New()
type Test struct {
@ -601,12 +533,11 @@ func BenchmarkStructPartialSuccess(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.StructPartial(test, "Name")
_ = validate.StructPartial(test, "Name")
}
}
func BenchmarkStructPartialSuccessParallel(b *testing.B) {
validate := New()
type Test struct {
@ -621,13 +552,12 @@ func BenchmarkStructPartialSuccessParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.StructPartial(test, "Name")
_ = validate.StructPartial(test, "Name")
}
})
}
func BenchmarkStructPartialFailure(b *testing.B) {
validate := New()
type Test struct {
@ -641,12 +571,11 @@ func BenchmarkStructPartialFailure(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.StructPartial(test, "NickName")
_ = validate.StructPartial(test, "NickName")
}
}
func BenchmarkStructPartialFailureParallel(b *testing.B) {
validate := New()
type Test struct {
@ -661,13 +590,12 @@ func BenchmarkStructPartialFailureParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.StructPartial(test, "NickName")
_ = validate.StructPartial(test, "NickName")
}
})
}
func BenchmarkStructExceptSuccess(b *testing.B) {
validate := New()
type Test struct {
@ -681,12 +609,11 @@ func BenchmarkStructExceptSuccess(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.StructExcept(test, "Nickname")
_ = validate.StructExcept(test, "Nickname")
}
}
func BenchmarkStructExceptSuccessParallel(b *testing.B) {
validate := New()
type Test struct {
@ -701,13 +628,12 @@ func BenchmarkStructExceptSuccessParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.StructExcept(test, "NickName")
_ = validate.StructExcept(test, "NickName")
}
})
}
func BenchmarkStructExceptFailure(b *testing.B) {
validate := New()
type Test struct {
@ -721,12 +647,11 @@ func BenchmarkStructExceptFailure(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.StructExcept(test, "Name")
_ = validate.StructExcept(test, "Name")
}
}
func BenchmarkStructExceptFailureParallel(b *testing.B) {
validate := New()
type Test struct {
@ -741,13 +666,12 @@ func BenchmarkStructExceptFailureParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.StructExcept(test, "Name")
_ = validate.StructExcept(test, "Name")
}
})
}
func BenchmarkStructSimpleCrossFieldSuccess(b *testing.B) {
validate := New()
type Test struct {
@ -765,12 +689,11 @@ func BenchmarkStructSimpleCrossFieldSuccess(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Struct(test)
_ = validate.Struct(test)
}
}
func BenchmarkStructSimpleCrossFieldSuccessParallel(b *testing.B) {
validate := New()
type Test struct {
@ -789,13 +712,12 @@ func BenchmarkStructSimpleCrossFieldSuccessParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Struct(test)
_ = validate.Struct(test)
}
})
}
func BenchmarkStructSimpleCrossFieldFailure(b *testing.B) {
validate := New()
type Test struct {
@ -813,12 +735,11 @@ func BenchmarkStructSimpleCrossFieldFailure(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Struct(test)
_ = validate.Struct(test)
}
}
func BenchmarkStructSimpleCrossFieldFailureParallel(b *testing.B) {
validate := New()
type Test struct {
@ -836,13 +757,12 @@ func BenchmarkStructSimpleCrossFieldFailureParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Struct(test)
_ = validate.Struct(test)
}
})
}
func BenchmarkStructSimpleCrossStructCrossFieldSuccess(b *testing.B) {
validate := New()
type Inner struct {
@ -867,12 +787,11 @@ func BenchmarkStructSimpleCrossStructCrossFieldSuccess(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Struct(outer)
_ = validate.Struct(outer)
}
}
func BenchmarkStructSimpleCrossStructCrossFieldSuccessParallel(b *testing.B) {
validate := New()
type Inner struct {
@ -898,13 +817,12 @@ func BenchmarkStructSimpleCrossStructCrossFieldSuccessParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Struct(outer)
_ = validate.Struct(outer)
}
})
}
func BenchmarkStructSimpleCrossStructCrossFieldFailure(b *testing.B) {
validate := New()
type Inner struct {
@ -930,12 +848,11 @@ func BenchmarkStructSimpleCrossStructCrossFieldFailure(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Struct(outer)
_ = validate.Struct(outer)
}
}
func BenchmarkStructSimpleCrossStructCrossFieldFailureParallel(b *testing.B) {
validate := New()
type Inner struct {
@ -962,13 +879,12 @@ func BenchmarkStructSimpleCrossStructCrossFieldFailureParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Struct(outer)
_ = validate.Struct(outer)
}
})
}
func BenchmarkStructSimpleSuccess(b *testing.B) {
validate := New()
type Foo struct {
@ -980,12 +896,11 @@ func BenchmarkStructSimpleSuccess(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Struct(validFoo)
_ = validate.Struct(validFoo)
}
}
func BenchmarkStructSimpleSuccessParallel(b *testing.B) {
validate := New()
type Foo struct {
@ -998,13 +913,12 @@ func BenchmarkStructSimpleSuccessParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Struct(validFoo)
_ = validate.Struct(validFoo)
}
})
}
func BenchmarkStructSimpleFailure(b *testing.B) {
validate := New()
type Foo struct {
@ -1016,12 +930,11 @@ func BenchmarkStructSimpleFailure(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Struct(invalidFoo)
_ = validate.Struct(invalidFoo)
}
}
func BenchmarkStructSimpleFailureParallel(b *testing.B) {
validate := New()
type Foo struct {
@ -1034,13 +947,12 @@ func BenchmarkStructSimpleFailureParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Struct(invalidFoo)
_ = validate.Struct(invalidFoo)
}
})
}
func BenchmarkStructComplexSuccess(b *testing.B) {
validate := New()
tSuccess := &TestString{
@ -1072,12 +984,11 @@ func BenchmarkStructComplexSuccess(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Struct(tSuccess)
_ = validate.Struct(tSuccess)
}
}
func BenchmarkStructComplexSuccessParallel(b *testing.B) {
validate := New()
tSuccess := &TestString{
@ -1110,13 +1021,12 @@ func BenchmarkStructComplexSuccessParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Struct(tSuccess)
_ = validate.Struct(tSuccess)
}
})
}
func BenchmarkStructComplexFailure(b *testing.B) {
validate := New()
tFail := &TestString{
@ -1145,12 +1055,11 @@ func BenchmarkStructComplexFailure(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
validate.Struct(tFail)
_ = validate.Struct(tFail)
}
}
func BenchmarkStructComplexFailureParallel(b *testing.B) {
validate := New()
tFail := &TestString{
@ -1180,7 +1089,7 @@ func BenchmarkStructComplexFailureParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
validate.Struct(tFail)
_ = validate.Struct(tFail)
}
})
}
@ -1193,7 +1102,7 @@ func BenchmarkOneof(b *testing.B) {
w := &TestOneof{Color: "green"}
val := New()
for i := 0; i < b.N; i++ {
val.Struct(w)
_ = val.Struct(w)
}
}
@ -1204,7 +1113,7 @@ func BenchmarkOneofParallel(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
val.Struct(w)
_ = val.Struct(w)
}
})
}

@ -125,25 +125,20 @@ func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStr
var customName string
for i := 0; i < numFields; i++ {
fld = typ.Field(i)
if !fld.Anonymous && len(fld.PkgPath) > 0 {
continue
}
tag = fld.Tag.Get(v.tagName)
if tag == skipValidationTag {
continue
}
customName = fld.Name
if v.hasTagNameFunc {
name := v.tagNameFunc(fld)
if len(name) > 0 {
customName = name
}
@ -157,9 +152,8 @@ func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStr
} else {
// even if field doesn't have validations need cTag for traversing to potential inner/nested
// elements of the field.
ctag = new(cTag)
ctag = &cTag{typeof: typeDefault}
}
cs.fields = append(cs.fields, &cField{
idx: i,
name: fld.Name,
@ -168,14 +162,11 @@ func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStr
namesEqual: fld.Name == customName,
})
}
v.structCache.Set(typ, cs)
return cs
}
func (v *Validate) parseFieldTagsRecursive(tag string, fieldName string, alias string, hasAlias bool) (firstCtag *cTag, current *cTag) {
var t string
var ok bool
noAlias := len(alias) == 0
@ -214,7 +205,6 @@ func (v *Validate) parseFieldTagsRecursive(tag string, fieldName string, alias s
}
switch t {
case diveTag:
current.typeof = typeDive
continue

@ -4,7 +4,7 @@ import (
"reflect"
"strings"
"github.com/andreiavrammsd/validator"
"gopkg.in/go-playground/validator.v9"
)
// NotBlank is the validation function for validating if the current field

@ -3,8 +3,8 @@ package validators
import (
"testing"
"github.com/andreiavrammsd/validator"
"gopkg.in/go-playground/assert.v1"
"gopkg.in/go-playground/validator.v9"
)
type test struct {

@ -376,7 +376,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
}
t, err = ut.T("ne-items", fe.Field(), c)
break
default:
t, err = ut.T("ne", fe.Field(), fe.Param())
}

@ -9,7 +9,7 @@ import (
"time"
"github.com/go-playground/locales"
"github.com/go-playground/universal-translator"
ut "github.com/go-playground/universal-translator"
"gopkg.in/go-playground/validator.v9"
)
@ -430,6 +430,9 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
case reflect.Struct:
if fe.Type() != reflect.TypeOf(time.Time{}) {
err = fmt.Errorf("tag '%s'不能用于struct类型.", fe.Tag())
if err != nil {
goto END
}
}
t, err = ut.T("lt-datetime", fe.Field())
@ -549,6 +552,9 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
case reflect.Struct:
if fe.Type() != reflect.TypeOf(time.Time{}) {
err = fmt.Errorf("tag '%s'不能用于struct类型.", fe.Tag())
if err != nil {
goto END
}
}
t, err = ut.T("lte-datetime", fe.Field())
@ -668,6 +674,9 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
case reflect.Struct:
if fe.Type() != reflect.TypeOf(time.Time{}) {
err = fmt.Errorf("tag '%s'不能用于struct类型.", fe.Tag())
if err != nil {
goto END
}
}
t, err = ut.T("gt-datetime", fe.Field())
@ -787,6 +796,9 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
case reflect.Struct:
if fe.Type() != reflect.TypeOf(time.Time{}) {
err = fmt.Errorf("tag '%s'不能用于struct类型.", fe.Tag())
if err != nil {
goto END
}
}
t, err = ut.T("gte-datetime", fe.Field())

@ -5,7 +5,7 @@ import (
"time"
zhongwen "github.com/go-playground/locales/zh"
"github.com/go-playground/universal-translator"
ut "github.com/go-playground/universal-translator"
. "gopkg.in/go-playground/assert.v1"
"gopkg.in/go-playground/validator.v9"
)
@ -15,6 +15,7 @@ func TestTranslations(t *testing.T) {
zh := zhongwen.New()
uni := ut.New(zh, zh)
trans, ok := uni.GetTranslator("zh")
Equal(t, ok, true)
validate := validator.New()

@ -9,7 +9,7 @@ import (
"time"
"github.com/go-playground/locales"
"github.com/go-playground/universal-translator"
ut "github.com/go-playground/universal-translator"
"gopkg.in/go-playground/validator.v9"
)
@ -430,6 +430,9 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
case reflect.Struct:
if fe.Type() != reflect.TypeOf(time.Time{}) {
err = fmt.Errorf("tag '%s'不能用於struct類型.", fe.Tag())
if err != nil {
goto END
}
}
t, err = ut.T("lt-datetime", fe.Field())
@ -549,6 +552,9 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
case reflect.Struct:
if fe.Type() != reflect.TypeOf(time.Time{}) {
err = fmt.Errorf("tag '%s'不能用於struct類型.", fe.Tag())
if err != nil {
goto END
}
}
t, err = ut.T("lte-datetime", fe.Field())
@ -668,6 +674,9 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
case reflect.Struct:
if fe.Type() != reflect.TypeOf(time.Time{}) {
err = fmt.Errorf("tag '%s'不能用於struct類型.", fe.Tag())
if err != nil {
goto END
}
}
t, err = ut.T("gt-datetime", fe.Field())
@ -787,6 +796,9 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
case reflect.Struct:
if fe.Type() != reflect.TypeOf(time.Time{}) {
err = fmt.Errorf("tag '%s'不能用於struct類型.", fe.Tag())
if err != nil {
goto END
}
}
t, err = ut.T("gte-datetime", fe.Field())

@ -5,16 +5,16 @@ import (
"time"
zhongwen "github.com/go-playground/locales/zh_Hant_TW"
"github.com/go-playground/universal-translator"
ut "github.com/go-playground/universal-translator"
. "gopkg.in/go-playground/assert.v1"
"gopkg.in/go-playground/validator.v9"
)
func TestTranslations(t *testing.T) {
zh := zhongwen.New()
uni := ut.New(zh, zh)
trans, ok := uni.GetTranslator("zh")
trans, ok := uni.GetTranslator("zh_Hant_TW")
Equal(t, ok, true)
validate := validator.New()

@ -439,9 +439,6 @@ func TestAnonymous(t *testing.T) {
AnonymousB struct {
B string `validate:"required" json:"BEE"`
}
anonymousC struct {
c string `validate:"required"`
}
}
tst := &Test{
@ -455,11 +452,6 @@ func TestAnonymous(t *testing.T) {
}{
B: "",
},
anonymousC: struct {
c string `validate:"required"`
}{
c: "",
},
}
err := validate.Struct(tst)
@ -715,12 +707,12 @@ func TestNilValidator(t *testing.T) {
}
PanicMatches(t, func() { val.RegisterCustomTypeFunc(ValidateCustomType, MadeUpCustomType{}) }, "runtime error: invalid memory address or nil pointer dereference")
PanicMatches(t, func() { val.RegisterValidation("something", fn) }, "runtime error: invalid memory address or nil pointer dereference")
PanicMatches(t, func() { val.Var(ts.Test, "required") }, "runtime error: invalid memory address or nil pointer dereference")
PanicMatches(t, func() { val.VarWithValue("test", ts.Test, "required") }, "runtime error: invalid memory address or nil pointer dereference")
PanicMatches(t, func() { val.Struct(ts) }, "runtime error: invalid memory address or nil pointer dereference")
PanicMatches(t, func() { val.StructExcept(ts, "Test") }, "runtime error: invalid memory address or nil pointer dereference")
PanicMatches(t, func() { val.StructPartial(ts, "Test") }, "runtime error: invalid memory address or nil pointer dereference")
PanicMatches(t, func() { _ = val.RegisterValidation("something", fn) }, "runtime error: invalid memory address or nil pointer dereference")
PanicMatches(t, func() { _ = val.Var(ts.Test, "required") }, "runtime error: invalid memory address or nil pointer dereference")
PanicMatches(t, func() { _ = val.VarWithValue("test", ts.Test, "required") }, "runtime error: invalid memory address or nil pointer dereference")
PanicMatches(t, func() { _ = val.Struct(ts) }, "runtime error: invalid memory address or nil pointer dereference")
PanicMatches(t, func() { _ = val.StructExcept(ts, "Test") }, "runtime error: invalid memory address or nil pointer dereference")
PanicMatches(t, func() { _ = val.StructPartial(ts, "Test") }, "runtime error: invalid memory address or nil pointer dereference")
}
func TestStructPartial(t *testing.T) {
@ -1850,7 +1842,7 @@ func TestSQLValue2Validation(t *testing.T) {
val.Name = "errorme"
PanicMatches(t, func() { validate.Var(val, "required") }, "SQL Driver Valuer error: some kind of error")
PanicMatches(t, func() { _ = validate.Var(val, "required") }, "SQL Driver Valuer error: some kind of error")
myVal := valuer{
Name: "",
@ -2675,7 +2667,7 @@ func TestBadKeyValidation(t *testing.T) {
validate := New()
PanicMatches(t, func() { validate.Struct(tst) }, "Undefined validation function ' ' on field 'Name'")
PanicMatches(t, func() { _ = validate.Struct(tst) }, "Undefined validation function ' ' on field 'Name'")
type Test2 struct {
Name string `validate:"required,,len=2"`
@ -2685,7 +2677,7 @@ func TestBadKeyValidation(t *testing.T) {
Name: "test",
}
PanicMatches(t, func() { validate.Struct(tst2) }, "Invalid validation tag on field 'Name'")
PanicMatches(t, func() { _ = validate.Struct(tst2) }, "Invalid validation tag on field 'Name'")
}
func TestInterfaceErrValidation(t *testing.T) {
@ -3009,7 +3001,7 @@ func TestArrayDiveValidation(t *testing.T) {
Name: "TEST",
}
PanicMatches(t, func() { validate.Struct(bd) }, "dive error! can't dive on a non slice or map")
PanicMatches(t, func() { _ = validate.Struct(bd) }, "dive error! can't dive on a non slice or map")
type Test struct {
Errs []string `validate:"gt=0,dive,required"`
@ -3372,7 +3364,7 @@ func TestLongitudeValidation(t *testing.T) {
}
}
PanicMatches(t, func() { validate.Var(true, "longitude") }, "Bad field type bool")
PanicMatches(t, func() { _ = validate.Var(true, "longitude") }, "Bad field type bool")
}
func TestLatitudeValidation(t *testing.T) {
@ -3414,7 +3406,7 @@ func TestLatitudeValidation(t *testing.T) {
}
}
PanicMatches(t, func() { validate.Var(true, "latitude") }, "Bad field type bool")
PanicMatches(t, func() { _ = validate.Var(true, "latitude") }, "Bad field type bool")
}
func TestDataURIValidation(t *testing.T) {
@ -4317,7 +4309,7 @@ func TestIsNeValidation(t *testing.T) {
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "ne")
PanicMatches(t, func() { validate.Var(now, "ne=now") }, "Bad field type time.Time")
PanicMatches(t, func() { _ = validate.Var(now, "ne=now") }, "Bad field type time.Time")
}
func TestIsEqFieldValidation(t *testing.T) {
@ -4471,7 +4463,7 @@ func TestIsEqValidation(t *testing.T) {
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "eq")
PanicMatches(t, func() { validate.Var(now, "eq=now") }, "Bad field type time.Time")
PanicMatches(t, func() { _ = validate.Var(now, "eq=now") }, "Bad field type time.Time")
}
func TestOneOfValidation(t *testing.T) {
@ -4530,7 +4522,7 @@ func TestOneOfValidation(t *testing.T) {
}
PanicMatches(t, func() {
validate.Var(3.14, "oneof=red green")
_ = validate.Var(3.14, "oneof=red green")
}, "Bad field type float64")
}
@ -4634,7 +4626,7 @@ func TestFileValidation(t *testing.T) {
}
PanicMatches(t, func() {
validate.Var(6, "file")
_ = validate.Var(6, "file")
}, "Bad field type int")
}
@ -5737,7 +5729,8 @@ func TestValidateByTagAndValue(t *testing.T) {
return fl.Parent().String() == fl.Field().String()
}
validate.RegisterValidation("isequaltestfunc", fn)
err := validate.RegisterValidation("isequaltestfunc", fn)
Equal(t, err, nil)
errs = validate.VarWithValue(val, field, "isequaltestfunc")
Equal(t, errs, nil)
@ -5768,7 +5761,7 @@ func TestAddFunctions(t *testing.T) {
errs = validate.RegisterValidation("", fn)
NotEqual(t, errs, nil)
validate.RegisterValidation("new", nil)
errs = validate.RegisterValidation("new", nil)
NotEqual(t, errs, nil)
errs = validate.RegisterValidation("new", fn)
@ -5777,7 +5770,7 @@ func TestAddFunctions(t *testing.T) {
errs = validate.RegisterValidationCtx("new", fnCtx)
Equal(t, errs, nil)
PanicMatches(t, func() { validate.RegisterValidation("dive", fn) }, "Tag 'dive' either contains restricted characters or is the same as a restricted tag needed for normal operation")
PanicMatches(t, func() { _ = validate.RegisterValidation("dive", fn) }, "Tag 'dive' either contains restricted characters or is the same as a restricted tag needed for normal operation")
}
func TestChangeTag(t *testing.T) {
@ -5819,6 +5812,7 @@ func TestUnexposedStruct(t *testing.T) {
errs := validate.Struct(s)
Equal(t, errs, nil)
Equal(t, s.unexposed.A, "")
}
func TestBadParams(t *testing.T) {
@ -5829,14 +5823,14 @@ func TestBadParams(t *testing.T) {
errs := validate.Var(i, "-")
Equal(t, errs, nil)
PanicMatches(t, func() { validate.Var(i, "len=a") }, "strconv.ParseInt: parsing \"a\": invalid syntax")
PanicMatches(t, func() { validate.Var(i, "len=a") }, "strconv.ParseInt: parsing \"a\": invalid syntax")
PanicMatches(t, func() { _ = validate.Var(i, "len=a") }, "strconv.ParseInt: parsing \"a\": invalid syntax")
PanicMatches(t, func() { _ = validate.Var(i, "len=a") }, "strconv.ParseInt: parsing \"a\": invalid syntax")
var ui uint = 1
PanicMatches(t, func() { validate.Var(ui, "len=a") }, "strconv.ParseUint: parsing \"a\": invalid syntax")
PanicMatches(t, func() { _ = validate.Var(ui, "len=a") }, "strconv.ParseUint: parsing \"a\": invalid syntax")
f := 1.23
PanicMatches(t, func() { validate.Var(f, "len=a") }, "strconv.ParseFloat: parsing \"a\": invalid syntax")
PanicMatches(t, func() { _ = validate.Var(f, "len=a") }, "strconv.ParseFloat: parsing \"a\": invalid syntax")
}
func TestLength(t *testing.T) {
@ -5844,7 +5838,7 @@ func TestLength(t *testing.T) {
validate := New()
i := true
PanicMatches(t, func() { validate.Var(i, "len") }, "Bad field type bool")
PanicMatches(t, func() { _ = validate.Var(i, "len") }, "Bad field type bool")
}
func TestIsGt(t *testing.T) {
@ -5866,7 +5860,7 @@ func TestIsGt(t *testing.T) {
AssertError(t, errs, "", "", "", "", "gt")
i := true
PanicMatches(t, func() { validate.Var(i, "gt") }, "Bad field type bool")
PanicMatches(t, func() { _ = validate.Var(i, "gt") }, "Bad field type bool")
tm := time.Now().UTC()
tm = tm.Add(time.Hour * 24)
@ -5904,7 +5898,7 @@ func TestIsGte(t *testing.T) {
validate := New()
i := true
PanicMatches(t, func() { validate.Var(i, "gte") }, "Bad field type bool")
PanicMatches(t, func() { _ = validate.Var(i, "gte") }, "Bad field type bool")
t1 := time.Now().UTC()
t1 = t1.Add(time.Hour * 24)
@ -5957,7 +5951,7 @@ func TestIsLt(t *testing.T) {
AssertError(t, errs, "", "", "", "", "lt")
i := true
PanicMatches(t, func() { validate.Var(i, "lt") }, "Bad field type bool")
PanicMatches(t, func() { _ = validate.Var(i, "lt") }, "Bad field type bool")
t1 := time.Now().UTC().Add(-time.Hour)
@ -5996,7 +5990,7 @@ func TestIsLte(t *testing.T) {
validate := New()
i := true
PanicMatches(t, func() { validate.Var(i, "lte") }, "Bad field type bool")
PanicMatches(t, func() { _ = validate.Var(i, "lte") }, "Bad field type bool")
t1 := time.Now().UTC().Add(-time.Hour)
@ -6103,7 +6097,7 @@ func TestUrnRFC2141(t *testing.T) {
}
i := 1
PanicMatches(t, func() { validate.Var(i, tag) }, "Bad field type int")
PanicMatches(t, func() { _ = validate.Var(i, tag) }, "Bad field type int")
}
func TestUrl(t *testing.T) {
@ -6171,7 +6165,7 @@ func TestUrl(t *testing.T) {
}
i := 1
PanicMatches(t, func() { validate.Var(i, "url") }, "Bad field type int")
PanicMatches(t, func() { _ = validate.Var(i, "url") }, "Bad field type int")
}
func TestUri(t *testing.T) {
@ -6238,7 +6232,7 @@ func TestUri(t *testing.T) {
}
i := 1
PanicMatches(t, func() { validate.Var(i, "uri") }, "Bad field type int")
PanicMatches(t, func() { _ = validate.Var(i, "uri") }, "Bad field type int")
}
func TestOrTag(t *testing.T) {
@ -6280,8 +6274,8 @@ func TestOrTag(t *testing.T) {
s = "this is right, but a blank or isn't"
PanicMatches(t, func() { validate.Var(s, "rgb||len=13") }, "Invalid validation tag on field ''")
PanicMatches(t, func() { validate.Var(s, "rgb|rgbaa|len=13") }, "Undefined validation function 'rgbaa' on field ''")
PanicMatches(t, func() { _ = validate.Var(s, "rgb||len=13") }, "Invalid validation tag on field ''")
PanicMatches(t, func() { _ = validate.Var(s, "rgb|rgbaa|len=13") }, "Undefined validation function 'rgbaa' on field ''")
v2 := New()
v2.RegisterTagNameFunc(func(fld reflect.StructField) string {
@ -6351,13 +6345,12 @@ func TestHsla(t *testing.T) {
AssertError(t, errs, "", "", "", "", "hsla")
i := 1
validate.Var(i, "hsla")
errs = validate.Var(i, "hsla")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "hsla")
}
func TestHsl(t *testing.T) {
validate := New()
s := "hsl(360,100%,50%)"
@ -7053,7 +7046,7 @@ func TestInvalidValidatorFunction(t *testing.T) {
Test: "1",
}
PanicMatches(t, func() { validate.Var(s.Test, "zzxxBadFunction") }, "Undefined validation function 'zzxxBadFunction' on field ''")
PanicMatches(t, func() { _ = validate.Var(s.Test, "zzxxBadFunction") }, "Undefined validation function 'zzxxBadFunction' on field ''")
}
func TestCustomFieldName(t *testing.T) {
@ -7314,10 +7307,11 @@ func TestTranslationErrors(t *testing.T) {
uni := ut.New(en, en, fr.New())
trans, _ := uni.GetTranslator("en")
trans.Add("required", "{0} is a required field", false) // using translator outside of validator also
err := trans.Add("required", "{0} is a required field", false) // using translator outside of validator also
Equal(t, err, nil)
validate := New()
err := validate.RegisterTranslation("required", trans,
err = validate.RegisterTranslation("required", trans,
func(ut ut.Translator) (err error) {
// using this stype because multiple translation may have to be added for the full translation
@ -7766,31 +7760,36 @@ func TestFieldLevelName(t *testing.T) {
return name
})
validate.RegisterValidation("custom1", func(fl FieldLevel) bool {
err := validate.RegisterValidation("custom1", func(fl FieldLevel) bool {
res1 = fl.FieldName()
alt1 = fl.StructFieldName()
return true
})
validate.RegisterValidation("custom2", func(fl FieldLevel) bool {
Equal(t, err, nil)
err = validate.RegisterValidation("custom2", func(fl FieldLevel) bool {
res2 = fl.FieldName()
alt2 = fl.StructFieldName()
return true
})
validate.RegisterValidation("custom3", func(fl FieldLevel) bool {
Equal(t, err, nil)
err = validate.RegisterValidation("custom3", func(fl FieldLevel) bool {
res3 = fl.FieldName()
alt3 = fl.StructFieldName()
return true
})
validate.RegisterValidation("custom4", func(fl FieldLevel) bool {
Equal(t, err, nil)
err = validate.RegisterValidation("custom4", func(fl FieldLevel) bool {
res4 = fl.FieldName()
alt4 = fl.StructFieldName()
return true
})
validate.RegisterValidation("custom5", func(fl FieldLevel) bool {
Equal(t, err, nil)
err = validate.RegisterValidation("custom5", func(fl FieldLevel) bool {
res5 = fl.FieldName()
alt5 = fl.StructFieldName()
return true
})
Equal(t, err, nil)
test := Test{
String: "test",
@ -7813,7 +7812,6 @@ func TestFieldLevelName(t *testing.T) {
}
func TestValidateStructRegisterCtx(t *testing.T) {
var ctxVal string
fnCtx := func(ctx context.Context, fl FieldLevel) bool {
@ -7833,7 +7831,8 @@ func TestValidateStructRegisterCtx(t *testing.T) {
var tst Test
validate := New()
validate.RegisterValidationCtx("val", fnCtx)
err := validate.RegisterValidationCtx("val", fnCtx)
Equal(t, err, nil)
validate.RegisterStructValidationCtx(slFn, Test{})
ctx := context.WithValue(context.Background(), &ctxVal, "testval")
@ -8157,7 +8156,7 @@ func TestUniqueValidation(t *testing.T) {
}
}
}
PanicMatches(t, func() { validate.Var(1.0, "unique") }, "Bad field type float64")
PanicMatches(t, func() { _ = validate.Var(1.0, "unique") }, "Bad field type float64")
}
func TestHTMLValidation(t *testing.T) {
@ -8362,8 +8361,8 @@ func TestKeys(t *testing.T) {
// test bad tag definitions
PanicMatches(t, func() { validate.Var(map[string]string{"key": "val"}, "endkeys,dive,eq=val") }, "'endkeys' tag encountered without a corresponding 'keys' tag")
PanicMatches(t, func() { validate.Var(1, "keys,eq=1,endkeys") }, "'keys' tag must be immediately preceded by the 'dive' tag")
PanicMatches(t, func() { _ = validate.Var(map[string]string{"key": "val"}, "endkeys,dive,eq=val") }, "'endkeys' tag encountered without a corresponding 'keys' tag")
PanicMatches(t, func() { _ = validate.Var(1, "keys,eq=1,endkeys") }, "'keys' tag must be immediately preceded by the 'dive' tag")
// test custom tag name
validate = New()
@ -8400,7 +8399,7 @@ func TestKeysCustomValidation(t *testing.T) {
}
validate := New()
validate.RegisterValidation("lang_code", func(fl FieldLevel) bool {
err := validate.RegisterValidation("lang_code", func(fl FieldLevel) bool {
validLangCodes := map[LangCode]struct{}{
"en": {},
"es": {},
@ -8410,6 +8409,7 @@ func TestKeysCustomValidation(t *testing.T) {
_, ok := validLangCodes[fl.Field().Interface().(LangCode)]
return ok
})
Equal(t, err, nil)
label := Label{
"en": "Good morning!",
@ -8419,7 +8419,7 @@ func TestKeysCustomValidation(t *testing.T) {
"xxx": "",
}
err := validate.Struct(TestMapStructPtr{label})
err = validate.Struct(TestMapStructPtr{label})
NotEqual(t, err, nil)
errs := err.(ValidationErrors)
@ -8562,7 +8562,7 @@ func TestDirValidation(t *testing.T) {
}
PanicMatches(t, func() {
validate.Var(2, "dir")
_ = validate.Var(2, "dir")
}, "Bad field type int")
}

Loading…
Cancel
Save