Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch/cat 2542 add metadata #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 24 additions & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
id: orwell
type: library

name: Orwell
product: Paas
owners:
- [email protected]

description: >
Validator package for Go
authenticity: false
availability: false
confidentiality: company-internal
integrity: false
exposure:
- non-applicable

lifecycle: ga

vertical: finanzcheck
tags:
- validation
- go
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")
}
})
}