-
Notifications
You must be signed in to change notification settings - Fork 3
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
98 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,55 @@ | ||
package orwell | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// XGtIsOr func | ||
func (*Orwell) XGtIsOr(gtField interface{}, gtValue int, isField interface{}, isValue interface{}, ors ...interface{}) *xGtIsOr { | ||
return &xGtIsOr{ | ||
gtField: gtField, | ||
gtValue: gtValue, | ||
isField: isField, | ||
isValue: isValue, | ||
ors: ors, | ||
msg: fmt.Sprintf("Validation error for 'XGtOr' rule"), | ||
} | ||
} | ||
|
||
// xGtOr struct | ||
type xGtIsOr struct { | ||
gtField interface{} | ||
gtValue int | ||
isField interface{} | ||
isValue interface{} | ||
ors []interface{} | ||
msg string | ||
} | ||
|
||
// Apply rule | ||
func (r *xGtIsOr) Apply(value interface{}) error { | ||
if !NOE(value) { | ||
return nil | ||
} | ||
|
||
gtField, isNil := IsNil(r.gtField) | ||
if isNil || IsEmpty(gtField) { | ||
return nil | ||
} | ||
|
||
gtFieldInt64, err := ToInt64(gtField) | ||
if err != nil { | ||
return fmt.Errorf("%s: %s", r.msg, err.Error()) | ||
} | ||
|
||
if gtFieldInt64 > int64(r.gtValue) && DeepEqual(r.isField, r.isValue) { | ||
for _, or := range r.ors { | ||
if !NOE(or) { | ||
return nil | ||
} | ||
} | ||
return fmt.Errorf(r.msg) | ||
} | ||
|
||
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,38 @@ | ||
package orwell | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestApplyXGtIsOr(t *testing.T) { | ||
t.Run("gtField > gtValue, isField == isValue, ors nil, value not nil", func(t *testing.T) { | ||
r := &xGtIsOr{gtField: 10, gtValue: 9, isField: "test", isValue: "test", ors: nil} | ||
if err := r.Apply("not nil"); err != nil { | ||
t.Error("Expected valid") | ||
} | ||
}) | ||
t.Run("gtField > gtValue, isField == isValue, ors nil, value not nil", func(t *testing.T) { | ||
r := &xGtIsOr{gtField: 10, gtValue: 9, isField: "test", isValue: "test", ors: nil} | ||
if err := r.Apply(nil); err == nil { | ||
t.Error("Expected error") | ||
} | ||
}) | ||
t.Run("gtField > gtValue, isField == isValue, ors nil, value not nil", func(t *testing.T) { | ||
r := &xGtIsOr{gtField: 10, gtValue: 10, isField: 0, isValue: 0, ors: nil} | ||
if err := r.Apply(nil); err != nil { | ||
t.Error("Expected valid because gtField is not greater than igtValue") | ||
} | ||
}) | ||
t.Run("gtField > gtValue, isField == isValue, ors nil, value not nil", func(t *testing.T) { | ||
r := &xGtIsOr{gtField: 10, gtValue: 9, isField: 0, isValue: 1, ors: nil} | ||
if err := r.Apply(nil); err != nil { | ||
t.Error("Expected valid because isField and isValue are not equal") | ||
} | ||
}) | ||
t.Run("gtField > gtValue, isField == isValue, ors nil, value not nil", func(t *testing.T) { | ||
r := &xGtIsOr{gtField: 10, gtValue: 9, isField: 1, isValue: 1, ors: []interface{}{"test"}} | ||
if err := r.Apply(nil); err != nil { | ||
t.Error("Expected valid because ors is not empty") | ||
} | ||
}) | ||
} |