Skip to content

Commit

Permalink
add XGtIsOr rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Antipitch committed May 30, 2018
1 parent 55d5452 commit 320405a
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ if err != nil {
- `XAnd(arg interface{})` Required when arg is neither nil nor empty
- `XAndOr(arg interface{}, args ...interface{})` Required when arg is neither nil nor empty and all args are nil or empty
- `XGt(arg1 interface{}, arg2 int)` Required when arg1 is greater than arg2
- `XGtOr(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
- `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
4 changes: 4 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,7 @@ func LengthOf(value interface{}) (int, error) {
}
return 0, fmt.Errorf("Could not determine length of argument type %v", v.Kind())
}

func DeepEqual(s interface{}, c interface{}) bool {
return reflect.DeepEqual(s, c)
}
55 changes: 55 additions & 0 deletions xgtisor.go
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
}
38 changes: 38 additions & 0 deletions xgtisor_test.go
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")
}
})
}

0 comments on commit 320405a

Please sign in to comment.