Skip to content

Commit

Permalink
refactor: made behavior simple
Browse files Browse the repository at this point in the history
BREAKING CHANGE: using utf8.RuneCountInString by len, min and max
BREAKING CHANGE: removed `zero` from default tags (can use `empty` instead)
  • Loading branch information
utahta committed May 12, 2019
1 parent 2e1b95d commit 7839ca9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 104 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ go get -u github.com/utahta/go-validator
goos: darwin
goarch: amd64
pkg: github.com/utahta/go-validator
BenchmarkValidateVarSuccess-12 20000000 57.5 ns/op 0 B/op 0 allocs/op
BenchmarkValidateVarParallelSuccess-12 100000000 12.8 ns/op 0 B/op 0 allocs/op
BenchmarkValidateStructSuccess-12 10000000 184 ns/op 0 B/op 0 allocs/op
BenchmarkValidateStructParallelSuccess-12 50000000 34.2 ns/op 0 B/op 0 allocs/op
BenchmarkValidateStructComplexSuccess-12 1000000 1072 ns/op 32 B/op 3 allocs/op
BenchmarkValidateStructComplexParallelSuccess-12 10000000 216 ns/op 32 B/op 3 allocs/op
BenchmarkValidateVarFailure-12 10000000 173 ns/op 208 B/op 2 allocs/op
BenchmarkValidateVarSuccess-12 30000000 58.8 ns/op 0 B/op 0 allocs/op
BenchmarkValidateVarParallelSuccess-12 100000000 12.1 ns/op 0 B/op 0 allocs/op
BenchmarkValidateStructSuccess-12 10000000 187 ns/op 0 B/op 0 allocs/op
BenchmarkValidateStructParallelSuccess-12 50000000 36.8 ns/op 0 B/op 0 allocs/op
BenchmarkValidateStructComplexSuccess-12 1000000 1139 ns/op 32 B/op 3 allocs/op
BenchmarkValidateStructComplexParallelSuccess-12 10000000 222 ns/op 32 B/op 3 allocs/op
BenchmarkValidateVarFailure-12 10000000 177 ns/op 208 B/op 2 allocs/op
```
132 changes: 40 additions & 92 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type (
// Params has any parameters.
Params []string

//TODO: move context
v *Validator
}

Expand All @@ -33,7 +34,6 @@ var (
"required": hasValue,
"req": hasValue,
"empty": isZeroValue,
"zero": isZeroValue,
"alpha": isAlpha,
"alphanum": isAlphaNum,
"alphaunicode": isAlphaUnicode,
Expand Down Expand Up @@ -71,19 +71,21 @@ var (
"fullwidth": isFullWidth,
"halfwidth": isHalfWidth,

// has parameters
"len": length,
"length": length,
"range": length,
"min": minLength,
"max": maxLength,
"strlen": strLength,
"strlength": strLength,
"strmin": strMinLength,
"strmax": strMaxLength,
"runelen": strLength,
"runelength": strLength,
"or": or,
// has parameters.
"len": length,
"length": length,
"range": length,
"min": minLength,
"max": maxLength,
"or": or,

// DEPRECATED. these are expected to be removed entirely sometime in the future.
"strlen": length,
"strlength": length,
"strmin": minLength,
"strmax": maxLength,
"runelen": length,
"runelength": length,
}

defaultAdapters []Adapter
Expand Down Expand Up @@ -292,7 +294,14 @@ func minLength(_ context.Context, f Field, opt FuncOption) (bool, error) {

v := f.current
switch v.Kind() {
case reflect.String, reflect.Array, reflect.Map, reflect.Slice:
case reflect.String:
min, err := parseInt64(minStr)
if err != nil {
return false, err
}
return min <= int64(utf8.RuneCountInString(v.String())), nil

case reflect.Array, reflect.Map, reflect.Slice:
min, err := parseInt64(minStr)
if err != nil {
return false, err
Expand Down Expand Up @@ -333,7 +342,14 @@ func maxLength(_ context.Context, f Field, opt FuncOption) (bool, error) {

v := f.current
switch v.Kind() {
case reflect.String, reflect.Array, reflect.Map, reflect.Slice:
case reflect.String:
max, err := parseInt64(maxStr)
if err != nil {
return false, err
}
return int64(utf8.RuneCountInString(v.String())) <= max, nil

case reflect.Array, reflect.Map, reflect.Slice:
max, err := parseInt64(maxStr)
if err != nil {
return false, err
Expand Down Expand Up @@ -372,7 +388,14 @@ func eqLength(_ context.Context, f Field, opt FuncOption) (bool, error) {

v := f.current
switch v.Kind() {
case reflect.String, reflect.Array, reflect.Map, reflect.Slice:
case reflect.String:
i, err := parseInt64(str)
if err != nil {
return false, err
}
return int64(utf8.RuneCountInString(v.String())) == i, nil

case reflect.Array, reflect.Map, reflect.Slice:
i, err := parseInt64(str)
if err != nil {
return false, err
Expand Down Expand Up @@ -427,81 +450,6 @@ func length(ctx context.Context, f Field, opt FuncOption) (bool, error) {
return false, fmt.Errorf("invalid params len")
}

func strMinLength(_ context.Context, f Field, opt FuncOption) (bool, error) {
if len(opt.Params) != 1 {
return false, fmt.Errorf("invalid params len")
}

v := f.current
switch v.Kind() {
case reflect.String:
min, err := parseInt64(opt.Params[0])
if err != nil {
return false, err
}
return min <= int64(utf8.RuneCountInString(v.String())), nil
}
return false, nil
}

func strMaxLength(_ context.Context, f Field, opt FuncOption) (bool, error) {
if len(opt.Params) != 1 {
return false, fmt.Errorf("invalid params len")
}

v := f.current
switch v.Kind() {
case reflect.String:
max, err := parseInt64(opt.Params[0])
if err != nil {
return false, err
}
return int64(utf8.RuneCountInString(v.String())) <= max, nil
}
return false, nil
}

func strEqLength(_ context.Context, f Field, opt FuncOption) (bool, error) {
if len(opt.Params) != 1 {
return false, fmt.Errorf("invalid params len")
}

v := f.current
switch v.Kind() {
case reflect.String:
i, err := parseInt64(opt.Params[0])
if err != nil {
return false, err
}
return int64(utf8.RuneCountInString(v.String())) == i, nil
}
return false, nil
}

func strLength(ctx context.Context, f Field, opt FuncOption) (bool, error) {
switch len(opt.Params) {
case 1:
eq, err := strEqLength(ctx, f, FuncOption{Params: opt.Params})
if err != nil {
return false, err
}
return eq, nil

case 2:
min, err := strMinLength(ctx, f, FuncOption{Params: opt.Params[:1]})
if err != nil {
return false, err
}
max, err := strMaxLength(ctx, f, FuncOption{Params: opt.Params[1:]})
if err != nil {
return false, err
}
return min && max, nil

}
return false, fmt.Errorf("invalid params len")
}

func or(_ context.Context, f Field, opt FuncOption) (bool, error) {
if opt.v == nil {
return false, fmt.Errorf("validator is nil")
Expand Down
14 changes: 9 additions & 5 deletions func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ func Test_required(t *testing.T) {
}
}

func Test_zero(t *testing.T) {
func Test_empty(t *testing.T) {
t.Parallel()

type (
Cat struct {
Name string `valid:"zero"`
Name string `valid:"empty"`
}
)
const tag = "zero"
const tag = "empty"
v := validator.New()

testcases := []struct {
Expand Down Expand Up @@ -1490,6 +1490,8 @@ func Test_length_minmax(t *testing.T) {
}{
{"valid string min", v.ValidateVar("aa", tag), false},
{"valid string max", v.ValidateVar("aaa", tag), false},
{"valid multibyte string min", v.ValidateVar("ああ", tag), false},
{"valid multibyte string max", v.ValidateVar("あああ", tag), false},
{"valid int min", v.ValidateVar(2, tag), false},
{"valid int max", v.ValidateVar(3, tag), false},
{"valid uint min", v.ValidateVar(uint(2), tag), false},
Expand All @@ -1503,6 +1505,8 @@ func Test_length_minmax(t *testing.T) {

{"invalid string min", v.ValidateVar("a", tag), true},
{"invalid string max", v.ValidateVar("aaaa", tag), true},
{"invalid multibyte string min", v.ValidateVar("あ", tag), false},
{"invalid multibyte string max", v.ValidateVar("ああああ", tag), false},
{"invalid int min", v.ValidateVar(1, tag), true},
{"invalid int max", v.ValidateVar(4, tag), true},
{"invalid uint min", v.ValidateVar(uint(1), tag), true},
Expand Down Expand Up @@ -1551,13 +1555,15 @@ func Test_length_equal(t *testing.T) {
hasErr bool
}{
{"valid string", v.ValidateVar("aa", tag), false},
{"valid multibyte string", v.ValidateVar("ああ", tag), false},
{"valid int", v.ValidateVar(2, tag), false},
{"valid uint", v.ValidateVar(uint(2), tag), false},
{"valid float", v.ValidateVar(float32(2.0), tag), false},
{"valid slice", v.ValidateVar([]int{2, 2}, tag), false},
{"valid map", v.ValidateVar(map[int]int{1: 2, 2: 2}, tag), false},

{"invalid string", v.ValidateVar("a", tag), true},
{"invalid multibyte string", v.ValidateVar("あ", tag), false},
{"invalid int", v.ValidateVar(1, tag), true},
{"invalid uint", v.ValidateVar(uint(1), tag), true},
{"invalid float", v.ValidateVar(float32(1.0), tag), true},
Expand Down Expand Up @@ -1597,7 +1603,6 @@ func Test_strlength_minmax(t *testing.T) {

{"invalid string min", v.ValidateVar("あ", tag), true},
{"invalid string max", v.ValidateVar("ああああ", tag), true},
{"invalid int", v.ValidateVar(3, tag), true},
}

for _, tc := range testcases {
Expand Down Expand Up @@ -1637,7 +1642,6 @@ func Test_strlength_equal(t *testing.T) {
{"valid string", v.ValidateVar("ああ", tag), false},

{"invalid string", v.ValidateVar("あ", tag), true},
{"invalid int", v.ValidateVar(2, tag), true},
}

for _, tc := range testcases {
Expand Down

0 comments on commit 7839ca9

Please sign in to comment.