Skip to content

Commit

Permalink
Merge pull request #2 from maksymgaraiev/feature/xgtAndOr
Browse files Browse the repository at this point in the history
add xGtAndOr rule
  • Loading branch information
Antipitch authored May 10, 2019
2 parents 7994f4e + c9d2794 commit 64f5ec9
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ if err != nil {
- `XGt(arg1 interface{}, arg2 int)` Required when arg1 is greater than arg2
- `XGtEql(arg1 interface{}, arg2 int, arg3 interface{}, arg4 interface{})` Required when arg1 is greater than arg2 and arg3 is equal to arg4
- `XGtEqlOr(arg1 interface{}, arg2 int, arg3 interface{}, arg4 interface{}, args ...interface{})` Required when arg1 is greater than arg2, arg3 is equal to arg4 and all args are nil or empty
- `XGtAndOr(arg1 interface{}, arg2 int, arg3 interface{}, args ...interface{})` Required when arg1 is greater than arg2, arg3 is neither nil nor empty and all args are nil or empty
- `XGtOr(arg1 interface{}, arg2 int, args ...interface{})` Required when arg1 is greater than arg2 and all args are nil or empty
- `XLt(arg1 interface{}, arg2 int)` Required when arg1 is lower than arg2
- `XLtOr(arg1 interface{}, arg2 int, args ...interface{})` Required when arg1 is lower than arg2 and all args are nil or empty
Expand Down
53 changes: 53 additions & 0 deletions xgtandor.go
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
}
45 changes: 45 additions & 0 deletions xgtandor_test.go
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")
}
})
}

0 comments on commit 64f5ec9

Please sign in to comment.