Skip to content

Commit

Permalink
Merge 3f0763a into e04d503
Browse files Browse the repository at this point in the history
  • Loading branch information
mehran-prs committed Feb 7, 2020
2 parents e04d503 + 3f0763a commit f375b1e
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
5 changes: 5 additions & 0 deletions required.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ type requiredRule struct {
err Error
}

// When validate provided value by "required" rule just when the condition is true.
func (r requiredRule) When(condition bool) WhenRule {
return When(condition, r)
}

// Validate checks if the given value is valid or not.
func (r requiredRule) Validate(value interface{}) error {
value, isNil := Indirect(value)
Expand Down
10 changes: 10 additions & 0 deletions required_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ func TestRequired(t *testing.T) {
}
}

func TestRequiredRule_When(t *testing.T) {
r := Required.When(false)
err := Validate(nil, r)
assert.Nil(t, err)

r = Required.When(true)
err = Validate(nil, r)
assert.Equal(t, ErrRequired, err)
}

func TestNilOrNotEmpty(t *testing.T) {
s1 := "123"
s2 := ""
Expand Down
25 changes: 25 additions & 0 deletions when.go
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
}
52 changes: 52 additions & 0 deletions when_test.go
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)
}
}

0 comments on commit f375b1e

Please sign in to comment.