Skip to content

Commit

Permalink
Revert "Custom rule not applied to file's validation (#19)"
Browse files Browse the repository at this point in the history
This reverts commit a943d06.
  • Loading branch information
thedevsaddam committed Jan 24, 2018
1 parent a943d06 commit 67b6990
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 59 deletions.
4 changes: 0 additions & 4 deletions rules.go
Expand Up @@ -3,7 +3,6 @@ package govalidator
import (
"errors"
"fmt"
"mime/multipart"
"net/url"
"reflect"
"strconv"
Expand Down Expand Up @@ -48,9 +47,6 @@ func init() {
if value == nil {
return err
}
if _, ok := value.(multipart.File); ok {
return nil
}
rv := reflect.ValueOf(value)
switch rv.Kind() {
case reflect.String, reflect.Array, reflect.Slice, reflect.Map:
Expand Down
51 changes: 0 additions & 51 deletions validate_file_test.go
Expand Up @@ -88,54 +88,3 @@ func Test_validateFiles_message(t *testing.T) {
t.Error("failed custom message for file validation")
}
}

func Test_validateFiles_CustomRule(t *testing.T) {
req, err := buildMocFormReq()
if err != nil {
t.Error("request failed", err)
}

customRule1WasExecuted := false
isMultipartFile := false
AddCustomRule("customRule1", func(field string, rule string, message string, value interface{}) error {
customRule1WasExecuted = true
_, isMultipartFile = value.(multipart.File)
return nil
})

customRule2WasExecuted := false
isValueNil := false
AddCustomRule("customRule2", func(field string, rule string, message string, value interface{}) error {
customRule2WasExecuted = true
isValueNil = value == nil
return nil
})

rules := MapData{
"file:file": []string{"customRule1"},
"file:avatar": []string{"customRule2"},
}

opts := Options{
Request: req,
Rules: rules,
}

vd := New(opts)
vd.Validate()
if !customRule1WasExecuted {
t.Error("file validation performed without custom rule!")
}

if !isMultipartFile {
t.Error("passed to custom rule value is not file!")
}

if !customRule2WasExecuted {
t.Error("file validation performed without custom rule!")
}

if !isValueNil {
t.Error("passed to custom rule value is not nil!")
}
}
9 changes: 5 additions & 4 deletions validator.go
Expand Up @@ -93,11 +93,12 @@ func (v *Validator) Validate() url.Values {
if strings.HasPrefix(field, "file:") {
fld := strings.TrimPrefix(field, "file:")
file, _, _ := v.Opts.Request.FormFile(fld)
if file != nil {
validateFiles(v.Opts.Request, fld, rule, msg, errsBag)
validateCustomRules(fld, rule, msg, file, errsBag)
if file == nil {
if isContainRequiredField(rules) {
validateCustomRules(fld, rule, msg, "", errsBag)
}
} else {
validateCustomRules(fld, rule, msg, nil, errsBag)
validateFiles(v.Opts.Request, fld, rule, msg, errsBag)
}
} else {
// validate if custom rules exist
Expand Down

0 comments on commit 67b6990

Please sign in to comment.