-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package validation | ||
|
||
// When returns a validation rule that checks if the specified condition | ||
//is true, validate the value by the specified rules. | ||
func When(condition bool, rules ...Rule) WhenRule { | ||
return WhenRule{ | ||
condition: condition, | ||
rules: rules, | ||
} | ||
} | ||
|
||
// WhenRule is a validation rule that validate element if condition is true. | ||
type WhenRule struct { | ||
condition bool | ||
rules []Rule | ||
} | ||
|
||
// Validate checks if the condition is true, validate value by specified rules. | ||
func (r WhenRule) Validate(value interface{}) error { | ||
if r.condition { | ||
return Validate(value, r.rules...) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2016 Qiang Xue. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package validation | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func abcValidation(val string) bool { | ||
return val == "abc" | ||
} | ||
|
||
func TestWhen(t *testing.T) { | ||
abcRule := NewStringRule(abcValidation, "wrong_abc") | ||
validateMeRule := NewStringRule(validateMe, "wrong_me") | ||
|
||
tests := []struct { | ||
tag string | ||
condition bool | ||
value interface{} | ||
rules []Rule | ||
err string | ||
}{ | ||
// True condition | ||
{"t1.1", true, nil, []Rule{}, ""}, | ||
{"t1.2", true, "", []Rule{}, ""}, | ||
{"t1.3", true, "", []Rule{abcRule}, ""}, | ||
{"t1.4", true, 12, []Rule{Required}, ""}, | ||
{"t1.5", true, nil, []Rule{Required}, "cannot be blank"}, | ||
{"t1.6", true, "123", []Rule{abcRule}, "wrong_abc"}, | ||
{"t1.7", true, "abc", []Rule{abcRule}, ""}, | ||
{"t1.8", true, "abc", []Rule{abcRule, abcRule}, ""}, | ||
{"t1.9", true, "abc", []Rule{abcRule, validateMeRule}, "wrong_me"}, | ||
{"t1.10", true, "me", []Rule{abcRule, validateMeRule}, "wrong_abc"}, | ||
|
||
// False condition | ||
{"t2.1", false, "", []Rule{}, ""}, | ||
{"t2.2", false, "", []Rule{abcRule}, ""}, | ||
{"t2.3", false, "abc", []Rule{abcRule}, ""}, | ||
{"t2.4", false, "abc", []Rule{abcRule, abcRule}, ""}, | ||
{"t2.5", false, "abc", []Rule{abcRule, validateMeRule}, ""}, | ||
{"t2.6", false, "me", []Rule{abcRule, validateMeRule}, ""}, | ||
{"t2.7", false, "", []Rule{abcRule, validateMeRule}, ""}, | ||
} | ||
|
||
for _, test := range tests { | ||
err := Validate(test.value, When(test.condition, test.rules...)) | ||
assertError(t, test.err, err, test.tag) | ||
} | ||
} |