From 5ed518fcbec144c474b067c843db3b05537b1f44 Mon Sep 17 00:00:00 2001 From: franklin Date: Fri, 17 Mar 2023 08:31:34 +0100 Subject: [PATCH] revert: remove duplicate --- fend/rules.go | 2 +- fender_example_test.go | 16 ++++++++-------- rule/optional.go | 21 --------------------- rule/required.go | 21 --------------------- 4 files changed, 9 insertions(+), 51 deletions(-) diff --git a/fend/rules.go b/fend/rules.go index 43e3c19..a5590b7 100644 --- a/fend/rules.go +++ b/fend/rules.go @@ -14,7 +14,7 @@ func NewRules[T any](rules ...rule.Rule[T]) Rules[T] { func (r Rules[T]) Field(path string, value T, rules ...rule.Rule[T]) Fend { return func(ctx context.Context, mode Mode) error { - return fend(ctx, mode, "", value, rule.Rules[T](rules).Append(r...)...) + return fend(ctx, mode, path, value, rule.Rules[T](rules).Append(r...)...) } } diff --git a/fender_example_test.go b/fender_example_test.go index 6a24651..50fb858 100644 --- a/fender_example_test.go +++ b/fender_example_test.go @@ -12,8 +12,8 @@ import ( func ExampleAll() { err := fender.All( context.Background(), - fend.Field("one", "", rule.StringRequired, rule.StringMin(10)), - fend.Field("two", "", rule.StringRequired, rule.StringMin(10)), + fend.Field("one", "", rule.Required[string], rule.StringMin(10)), + fend.Field("two", "", rule.Required[string], rule.StringMin(10)), ) // check for fender error if fendErr := fender.AsError(err); fendErr != nil { @@ -27,8 +27,8 @@ func ExampleAll() { func ExampleFirst() { err := fender.First( context.Background(), - fend.Field("one", "", rule.StringRequired, rule.StringMin(10)), - fend.Field("two", "", rule.StringRequired, rule.StringMin(10)), + fend.Field("one", "", rule.Required[string], rule.StringMin(10)), + fend.Field("two", "", rule.Required[string], rule.StringMin(10)), ) // check for fender error @@ -43,8 +43,8 @@ func ExampleFirst() { func ExampleAllFirst() { err := fender.AllFirst( context.Background(), - fend.Field("one", "", rule.StringRequired, rule.StringMin(10)), - fend.Field("two", "", rule.StringRequired, rule.StringMin(10)), + fend.Field("one", "", rule.Required[string], rule.StringMin(10)), + fend.Field("two", "", rule.Required[string], rule.StringMin(10)), ) // check for fender error @@ -59,8 +59,8 @@ func ExampleAllFirst() { func ExampleErrors() { //nolint:govet err := fender.All( context.Background(), - fend.Field("one", "", rule.StringRequired, rule.StringMin(10)), - fend.Field("two", "", rule.StringRequired, rule.StringMin(10)), + fend.Field("one", "", rule.Required[string], rule.StringMin(10)), + fend.Field("two", "", rule.Required[string], rule.StringMin(10)), ) // cast fender error diff --git a/rule/optional.go b/rule/optional.go index 13afe89..52f961c 100644 --- a/rule/optional.go +++ b/rule/optional.go @@ -11,24 +11,3 @@ func Optional[T any](ctx context.Context, v T) error { } return nil } - -func BoolOptional(ctx context.Context, v bool) error { - if !v { - return ErrBreak - } - return nil -} - -func StringOptional(ctx context.Context, v string) error { - if len(v) == 0 { - return ErrBreak - } - return nil -} - -func NumberOptional[T Number](ctx context.Context, v T) error { - if v == T(0) { - return ErrBreak - } - return nil -} diff --git a/rule/required.go b/rule/required.go index 317520a..c836076 100644 --- a/rule/required.go +++ b/rule/required.go @@ -25,24 +25,3 @@ func IsRequired[T any](expected bool) Rule[T] { return nil } } - -func BoolRequired(ctx context.Context, v bool) error { - if !v { - return NewError(NameRequired) - } - return nil -} - -func StringRequired(ctx context.Context, v string) error { - if len(v) == 0 { - return NewError(NameRequired) - } - return nil -} - -func NumberRequired[T Number](ctx context.Context, v T) error { - if v == T(0) { - return NewError(NameRequired) - } - return nil -}