diff --git a/README.md b/README.md index 8ff029a..59b703a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/metadata.yaml b/metadata.yaml new file mode 100644 index 0000000..2ce73f0 --- /dev/null +++ b/metadata.yaml @@ -0,0 +1,24 @@ +--- +id: orwell +type: library + +name: Orwell +product: Paas +owners: + - team.catalyst@finanzcheck.de + +description: > + Validator package for Go +authenticity: false +availability: false +confidentiality: company-internal +integrity: false +exposure: + - non-applicable + +lifecycle: ga + +vertical: finanzcheck +tags: + - validation + - go \ No newline at end of file diff --git a/xgtandor.go b/xgtandor.go new file mode 100644 index 0000000..1e8c466 --- /dev/null +++ b/xgtandor.go @@ -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 +} diff --git a/xgtandor_test.go b/xgtandor_test.go new file mode 100644 index 0000000..e8e14d1 --- /dev/null +++ b/xgtandor_test.go @@ -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") + } + }) +}