Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Folders
_obj
_test
bin

# Architecture specific extensions/prefixes
*.[568vq]
Expand All @@ -26,4 +27,4 @@ _testmain.go
*.out
*.txt
cover.html
README.html
README.html
17 changes: 7 additions & 10 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ func isEq(fl FieldLevel) bool {
return int64(field.Len()) == p

case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
p := asInt(param)
p := asIntFromType(field.Type(), param)

return field.Int() == p

Expand Down Expand Up @@ -1541,7 +1541,7 @@ func isGte(fl FieldLevel) bool {
return int64(field.Len()) >= p

case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
p := asInt(param)
p := asIntFromType(field.Type(), param)

return field.Int() >= p

Expand Down Expand Up @@ -1588,7 +1588,7 @@ func isGt(fl FieldLevel) bool {
return int64(field.Len()) > p

case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
p := asInt(param)
p := asIntFromType(field.Type(), param)

return field.Int() > p

Expand Down Expand Up @@ -1631,7 +1631,7 @@ func hasLengthOf(fl FieldLevel) bool {
return int64(field.Len()) == p

case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
p := asInt(param)
p := asIntFromType(field.Type(), param)

return field.Int() == p

Expand Down Expand Up @@ -1767,7 +1767,7 @@ func isLte(fl FieldLevel) bool {
return int64(field.Len()) <= p

case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
p := asInt(param)
p := asIntFromType(field.Type(), param)

return field.Int() <= p

Expand Down Expand Up @@ -1814,7 +1814,7 @@ func isLt(fl FieldLevel) bool {
return int64(field.Len()) < p

case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
p := asInt(param)
p := asIntFromType(field.Type(), param)

return field.Int() < p

Expand Down Expand Up @@ -2087,11 +2087,8 @@ func isDatetime(fl FieldLevel) bool {

if field.Kind() == reflect.String {
_, err := time.Parse(param, field.String())
if err != nil {
return false
}

return true
return err == nil
}

panic(fmt.Sprintf("Bad field type %T", field.Interface()))
Expand Down
100 changes: 86 additions & 14 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,42 +321,87 @@ equal to the parameter given. For strings, it checks that
the string length is exactly that number of characters. For slices,
arrays, and maps, validates the number of items.

Example #1

Usage: len=10

Example #2 (time.Duration)

For time.Duration, len will ensure that the value is equal to the duration given
in the parameter.

Usage: len=1h30m

Maximum

For numbers, max will ensure that the value is
less than or equal to the parameter given. For strings, it checks
that the string length is at most that number of characters. For
slices, arrays, and maps, validates the number of items.

Example #1

Usage: max=10

Example #2 (time.Duration)

For time.Duration, max will ensure that the value is less than or equal to the
duration given in the parameter.

Usage: max=1h30m

Minimum

For numbers, min will ensure that the value is
greater or equal to the parameter given. For strings, it checks that
the string length is at least that number of characters. For slices,
arrays, and maps, validates the number of items.

Example #1

Usage: min=10

Example #2 (time.Duration)

For time.Duration, min will ensure that the value is greater than or equal to
the duration given in the parameter.

Usage: min=1h30m

Equals

For strings & numbers, eq will ensure that the value is
equal to the parameter given. For slices, arrays, and maps,
validates the number of items.

Example #1

Usage: eq=10

Example #2 (time.Duration)

For time.Duration, eq will ensure that the value is equal to the duration given
in the parameter.

Usage: eq=1h30m

Not Equal

For strings & numbers, ne will ensure that the value is not
equal to the parameter given. For slices, arrays, and maps,
validates the number of items.

Example #1

Usage: ne=10

Example #2 (time.Duration)

For time.Duration, ne will ensure that the value is not equal to the duration
given in the parameter.

Usage: ne=1h30m

One Of

For strings, ints, and uints, oneof will ensure that the value
Expand Down Expand Up @@ -386,11 +431,17 @@ For time.Time ensures the time value is greater than time.Now.UTC().

Usage: gt

Example #3 (time.Duration)

For time.Duration, gt will ensure that the value is greater than the duration
given in the parameter.

Usage: gt=1h30m

Greater Than or Equal

Same as 'min' above. Kept both to make terminology with 'len' easier.


Example #1

Usage: gte=10
Expand All @@ -401,6 +452,13 @@ For time.Time ensures the time value is greater than or equal to time.Now.UTC().

Usage: gte

Example #3 (time.Duration)

For time.Duration, gte will ensure that the value is greater than or equal to
the duration given in the parameter.

Usage: gte=1h30m

Less Than

For numbers, this will ensure that the value is less than the parameter given.
Expand All @@ -412,10 +470,18 @@ Example #1
Usage: lt=10

Example #2 (time.Time)

For time.Time ensures the time value is less than time.Now.UTC().

Usage: lt

Example #3 (time.Duration)

For time.Duration, lt will ensure that the value is less than the duration given
in the parameter.

Usage: lt=1h30m

Less Than or Equal

Same as 'max' above. Kept both to make terminology with 'len' easier.
Expand All @@ -430,6 +496,13 @@ For time.Time ensures the time value is less than or equal to time.Now.UTC().

Usage: lte

Example #3 (time.Duration)

For time.Duration, lte will ensure that the value is less than or equal to the
duration given in the parameter.

Usage: lte=1h30m

Field Equals Another Field

This will validate the field value against another fields value either within
Expand Down Expand Up @@ -476,9 +549,9 @@ relative to the top level struct.

Field Greater Than Another Field

Only valid for Numbers and time.Time types, this will validate the field value
against another fields value either within a struct or passed in field.
usage examples are for validation of a Start and End date:
Only valid for Numbers, time.Duration and time.Time types, this will validate
the field value against another fields value either within a struct or passed in
field. usage examples are for validation of a Start and End date:

Example #1:

Expand All @@ -490,7 +563,6 @@ Example #2:
// Validating by field:
validate.VarWithValue(start, end, "gtfield")


Field Greater Than Another Relative Field

This does the same as gtfield except that it validates the field provided
Expand All @@ -500,9 +572,9 @@ relative to the top level struct.

Field Greater Than or Equal To Another Field

Only valid for Numbers and time.Time types, this will validate the field value
against another fields value either within a struct or passed in field.
usage examples are for validation of a Start and End date:
Only valid for Numbers, time.Duration and time.Time types, this will validate
the field value against another fields value either within a struct or passed in
field. usage examples are for validation of a Start and End date:

Example #1:

Expand All @@ -523,9 +595,9 @@ to the top level struct.

Less Than Another Field

Only valid for Numbers and time.Time types, this will validate the field value
against another fields value either within a struct or passed in field.
usage examples are for validation of a Start and End date:
Only valid for Numbers, time.Duration and time.Time types, this will validate
the field value against another fields value either within a struct or passed in
field. usage examples are for validation of a Start and End date:

Example #1:

Expand All @@ -546,9 +618,9 @@ to the top level struct.

Less Than or Equal To Another Field

Only valid for Numbers and time.Time types, this will validate the field value
against another fields value either within a struct or passed in field.
usage examples are for validation of a Start and End date:
Only valid for Numbers, time.Duration and time.Time types, this will validate
the field value against another fields value either within a struct or passed in
field. usage examples are for validation of a Start and End date:

Example #1:

Expand Down
21 changes: 21 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"reflect"
"strconv"
"strings"
"time"
)

// extractTypeInternal gets the actual underlying type of field value.
Expand Down Expand Up @@ -229,6 +230,26 @@ func asInt(param string) int64 {
return i
}

// asIntFromTimeDuration parses param as time.Duration and returns it as int64
// or panics on error.
func asIntFromTimeDuration(param string) int64 {
d, err := time.ParseDuration(param)
panicIf(err)

return int64(d)
}

// asIntFromType calls the proper function to parse param as int64,
// given a field's Type t.
func asIntFromType(t reflect.Type, param string) int64 {
switch t {
case timeDurationType:
return asIntFromTimeDuration(param)
default:
return asInt(param)
}
}

// asUint returns the parameter as a uint64
// or panics if it can't convert
func asUint(param string) uint64 {
Expand Down
4 changes: 3 additions & 1 deletion validator_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ const (
)

var (
timeType = reflect.TypeOf(time.Time{})
timeDurationType = reflect.TypeOf(time.Duration(0))
timeType = reflect.TypeOf(time.Time{})

defaultCField = &cField{namesEqual: true}
)

Expand Down
Loading