-
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.
Co-authored-by: Кирилл Маликов <[email protected]>
- Loading branch information
Showing
7 changed files
with
351 additions
and
1 deletion.
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
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,81 @@ | ||
package validator | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type OR struct { | ||
rules []Rule | ||
message string | ||
whenFunc WhenFunc | ||
skipEmpty bool | ||
skipError bool | ||
} | ||
|
||
func NewOR(message string, rules ...Rule) *OR { | ||
return &OR{ | ||
message: message, | ||
rules: rules, | ||
} | ||
} | ||
|
||
func (o *OR) WithMessage(message string) *OR { | ||
rc := *o | ||
rc.message = message | ||
|
||
return &rc | ||
} | ||
|
||
func (o *OR) When(v WhenFunc) *OR { | ||
rc := *o | ||
rc.whenFunc = v | ||
|
||
return &rc | ||
} | ||
|
||
func (o *OR) when() WhenFunc { | ||
return o.whenFunc | ||
} | ||
|
||
func (o *OR) setWhen(v WhenFunc) { | ||
o.whenFunc = v | ||
} | ||
|
||
func (o *OR) SkipOnEmpty() *OR { | ||
rc := *o | ||
rc.skipEmpty = true | ||
|
||
return &rc | ||
} | ||
|
||
func (o *OR) skipOnEmpty() bool { | ||
return o.skipEmpty | ||
} | ||
|
||
func (o *OR) setSkipOnEmpty(v bool) { | ||
o.skipEmpty = v | ||
} | ||
|
||
func (o *OR) SkipOnError() *OR { | ||
rs := *o | ||
rs.skipError = true | ||
|
||
return &rs | ||
} | ||
|
||
func (o *OR) shouldSkipOnError() bool { | ||
return o.skipError | ||
} | ||
func (o *OR) setSkipOnError(v bool) { | ||
o.skipError = v | ||
} | ||
|
||
func (o *OR) ValidateValue(ctx context.Context, value any) error { | ||
for _, r := range o.rules { | ||
if err := r.ValidateValue(ctx, value); err == nil { | ||
return nil | ||
} | ||
} | ||
|
||
return NewResult().WithError(NewValidationError(o.message)) | ||
} |
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,80 @@ | ||
package validator | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestOR_ValidateValue_Successfully(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
type args struct { | ||
rules []Rule | ||
value any | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
}{ | ||
{ | ||
name: "ip or mac rules for ip value", | ||
args: args{ | ||
rules: []Rule{ | ||
NewIP(), | ||
NewMAC(), | ||
}, | ||
value: "127.0.0.1", | ||
}, | ||
}, | ||
{ | ||
name: "ip or mac rules for mac value", | ||
args: args{ | ||
rules: []Rule{ | ||
NewIP(), | ||
NewMAC(), | ||
}, | ||
value: "00:1b:63:84:45:e6", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
o := NewOR("Value is not in ip or mac format.", tt.args.rules...) | ||
err := o.ValidateValue(ctx, tt.args.value) | ||
require.NoError(t, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestOR_ValidateValue_Failure(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
type args struct { | ||
rules []Rule | ||
value any | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
}{ | ||
{ | ||
name: "ip or mac rules for invalid value", | ||
args: args{ | ||
rules: []Rule{ | ||
NewIP(), | ||
NewMAC(), | ||
}, | ||
value: "hello world", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
o := NewOR("Value is not in ip or mac format.", tt.args.rules...) | ||
err := o.ValidateValue(ctx, tt.args.value) | ||
require.Error(t, err) | ||
}) | ||
} | ||
} |
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
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