-
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.
Merge pull request #2 from maksymgaraiev/feature/xgtAndOr
add xGtAndOr rule
- Loading branch information
Showing
3 changed files
with
99 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package orwell | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// XGtAndOr func | ||
func (*Orwell) XGtAndOr(gtField interface{}, gtValue int, and interface{}, ors ...interface{}) *xGtAndOr { | ||
return &xGtAndOr{ | ||
gtField: gtField, | ||
gtValue: gtValue, | ||
and: and, | ||
ors: ors, | ||
msg: fmt.Sprintf("Validation error for 'XGtAndOr' rule"), | ||
} | ||
} | ||
|
||
// xGtAndOr struct | ||
type xGtAndOr struct { | ||
gtField interface{} | ||
gtValue int | ||
and interface{} | ||
ors []interface{} | ||
msg string | ||
} | ||
|
||
// Apply rule | ||
func (r *xGtAndOr) 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) && !NOE(r.and) { | ||
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,45 @@ | ||
package orwell | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestApplyXGtAndOr(t *testing.T) { | ||
t.Run("gtField > gtValue, andValue not empty, ors nil, value not nil", func(t *testing.T) { | ||
r := &xGtAndOr{gtField: 10, gtValue: 9, and: "not nil", ors: nil} | ||
if err := r.Apply("not nil"); err != nil { | ||
t.Error("Expected valid because validated value is not nil ") | ||
} | ||
}) | ||
t.Run("gtField > gtValue, andValue not empty, ors nil, value not nil", func(t *testing.T) { | ||
r := &xGtAndOr{gtField: 10, gtValue: 9, and: "not nil", ors: nil} | ||
if err := r.Apply(nil); err == nil { | ||
t.Error("Expected error because validated value is nil") | ||
} | ||
}) | ||
t.Run("gtField > gtValue, andValue is empty, ors nil, value not nil", func(t *testing.T) { | ||
r := &xGtAndOr{gtField: 10, gtValue: 9, and: nil, ors: nil} | ||
if err := r.Apply(nil); err != nil { | ||
t.Error("Expected valid because andValue is nil") | ||
} | ||
}) | ||
t.Run("gtField > gtValue, andValue is empty, ors nil, value not nil", func(t *testing.T) { | ||
r := &xGtAndOr{gtField: 10, gtValue: 9, and: "", ors: nil} | ||
if err := r.Apply(nil); err != nil { | ||
t.Error("Expected valid because andValue is empty") | ||
} | ||
}) | ||
t.Run("gtField = gtValue, andValue not empty, ors nil, value not nil", func(t *testing.T) { | ||
r := &xGtAndOr{gtField: 10, gtValue: 10, and: "not nil", ors: nil} | ||
if err := r.Apply(nil); err != nil { | ||
t.Error("Expected valid because gtField is not greater than gtValue") | ||
} | ||
}) | ||
|
||
t.Run("gtField > gtValue, andValue not empty, ors nil, value not nil", func(t *testing.T) { | ||
r := &xGtAndOr{gtField: 10, gtValue: 9, and: "not nil", ors: []interface{}{"test"}} | ||
if err := r.Apply(nil); err != nil { | ||
t.Error("Expected valid because ors is not empty") | ||
} | ||
}) | ||
} |