Skip to content

Commit

Permalink
validator_test: add test cases for missing validator in ValidateMap
Browse files Browse the repository at this point in the history
  • Loading branch information
mie00 committed May 10, 2019
1 parent 9abeb39 commit 5c4ddb2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,8 @@ func ValidateMap(s map[string]interface{}, m map[string]interface{}) (bool, erro
if err != nil {
errs = append(errs, err)
}
case nil:
// already handlerd when checked before
default:
typeResult = false
err = fmt.Errorf("map validator has to be either map[string]interface{} or string; got %s", valueField.Type().String())
Expand Down
46 changes: 46 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3950,6 +3950,52 @@ func TestValidateMapMissing(t *testing.T) {
}
}

func TestValidateMapMissingValidator(t *testing.T) {
t.Parallel()
var tests = []struct {
params map[string]interface{}
validator map[string]interface{}
expected bool
}{
{
map[string]interface{}{
"name": "Bob",
"family": "Smith",
},
map[string]interface{}{
"name": "required,alpha",
},
false,
},
{
map[string]interface{}{
"name": "Bob",
"submap": map[string]interface{}{
"name": "Bob",
"family": "Smith",
},
},
map[string]interface{}{
"name": "required,alpha",
"submap": map[string]interface{}{
"family": "required,alpha",
},
},
false,
},
}

for _, test := range tests {
actual, err := ValidateMap(test.params, test.validator)
if actual != test.expected {
t.Errorf("Expected ValidateMap(%q, %q) to be %v, got %v", test.params, test.validator, test.expected, actual)
if err != nil {
t.Errorf("Got Error on ValidateMap(%q, %q): %s", test.params, test.validator, err)
}
}
}
}

func TestIsType(t *testing.T) {
t.Parallel()
i := 1
Expand Down

0 comments on commit 5c4ddb2

Please sign in to comment.