Fix time.Duration parsing for int param (#678)

This fixes an issue where if the param of a time.Durtion type is specified
as an integer, denoting nanosecond precision, instead of time duration
string the validation would panic.

the fixes ensures it falls back to the previous expected behaviour.
pull/668/merge v10.4.1
Dean Karn 4 years ago committed by GitHub
parent d6b17fd90b
commit 456221b630
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      README.md
  2. 6
      baked_in.go
  3. 7
      util.go
  4. 45
      validator_test.go

@ -1,7 +1,7 @@
Package validator
================
<img align="right" src="https://raw.githubusercontent.com/go-playground/validator/v9/logo.png">[![Join the chat at https://gitter.im/go-playground/validator](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/go-playground/validator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
![Project status](https://img.shields.io/badge/version-10.3.0-green.svg)
![Project status](https://img.shields.io/badge/version-10.4.1-green.svg)
[![Build Status](https://travis-ci.org/go-playground/validator.svg?branch=master)](https://travis-ci.org/go-playground/validator)
[![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=master&service=github)](https://coveralls.io/github/go-playground/validator?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator)

@ -2250,11 +2250,7 @@ func isTimeZone(fl FieldLevel) bool {
}
_, err := time.LoadLocation(field.String())
if err != nil {
return false
}
return true
return err == nil
}
panic(fmt.Sprintf("Bad field type %T", field.Interface()))

@ -223,7 +223,6 @@ BEGIN:
// asInt returns the parameter as a int64
// or panics if it can't convert
func asInt(param string) int64 {
i, err := strconv.ParseInt(param, 0, 64)
panicIf(err)
@ -234,8 +233,10 @@ func asInt(param string) int64 {
// or panics on error.
func asIntFromTimeDuration(param string) int64 {
d, err := time.ParseDuration(param)
panicIf(err)
if err != nil {
// attempt parsing as an an integer assuming nanosecond precision
return asInt(param)
}
return int64(d)
}

@ -9475,7 +9475,7 @@ func TestUniqueValidationStructPtrSlice(t *testing.T) {
}
}
}
PanicMatches(t, func() { validate.Var(testStructs, "unique=C") }, "Bad field name C")
PanicMatches(t, func() { _ = validate.Var(testStructs, "unique=C") }, "Bad field name C")
}
func TestHTMLValidation(t *testing.T) {
@ -10990,3 +10990,46 @@ func TestTimeZoneValidation(t *testing.T) {
_ = validate.Var(2, "timezone")
}, "Bad field type int")
}
func TestDurationType(t *testing.T) {
tests := []struct {
name string
s interface{} // struct
success bool
}{
{
name: "valid duration string pass",
s: struct {
Value time.Duration `validate:"gte=500ns"`
}{
Value: time.Second,
},
success: true,
},
{
name: "valid duration int pass",
s: struct {
Value time.Duration `validate:"gte=500"`
}{
Value: time.Second,
},
success: true,
},
}
validate := New()
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
errs := validate.Struct(tc.s)
if tc.success {
Equal(t, errs, nil)
return
}
NotEqual(t, errs, nil)
})
}
}

Loading…
Cancel
Save