From e57b84919b2fe70522b005cab829a174a9a09d5c Mon Sep 17 00:00:00 2001 From: Yury Miadzelets Date: Tue, 3 Dec 2024 11:59:50 +0300 Subject: [PATCH] fix panic when using typed When --- examples/translation/main.go | 20 ++++++++++++++------ num/base.go | 8 +------- shared/field.go | 1 + str/base.go | 8 +------- uuid/base.go | 4 +--- 5 files changed, 18 insertions(+), 23 deletions(-) diff --git a/examples/translation/main.go b/examples/translation/main.go index b288319..3014e1e 100644 --- a/examples/translation/main.go +++ b/examples/translation/main.go @@ -48,13 +48,19 @@ func main() { allowedClientID := uuid.New() valigo.Configure[Sender](v, func(builder valigo.Configurator[Sender], obj *Sender) { builder.Number(&obj.Int).AnyOf(2) - builder.String(&obj.Type).Trim() + builder.String(&obj.Type).When(func(ctx context.Context, value any) bool { + return true + }).Trim() + builder.String(&obj.Description).When(func(ctx context.Context, value any) bool { + return true + }).Trim() builder.String(&obj.SMTPHost).Trim(). Regexp(regexp.MustCompile("^[a-zA-Z0-9.]+$"), str.WithRegexpLocaleKey(customRegexpLocaleKey)) - builder.UUID(&obj.Id).Required() + builder.UUID(&obj.Id).When(func(ctx context.Context, value any) bool { + return true + }).AnyOf(uuid.Nil) builder.String(&obj.Email).Email() builder.StringSlice(&obj.Emails).Email() - builder.String(&obj.Description).Trim() builder.StringSlice(&obj.Templates).Trim(). Regexp(regexp.MustCompile("^[a-zA-Z0-9.]+$"), str.WithRegexpLocaleKey(customRegexpLocaleKey)) builder.UUIDSlice(&obj.ClientIDs).AnyOf(allowedClientID) @@ -63,8 +69,10 @@ func main() { if err != nil { panic(err) } + desc := "desc " sender := &Sender{ - Type: "123@123 ", + Type: "type ", + Description: &desc, Templates: []string{" correct ", "incorrect&"}, ClientIDs: []*uuid.UUID{&id}, SMTPHost: uuid.New().String() + " ", @@ -74,10 +82,10 @@ func main() { Email: "correct@email.com", Emails: []string{"correct@email.com", "incorrect.email.com"}, Id: id, - Int: 2, + Int: 5, } + errs := v.Validate(context.Background(), sender) errsJson, _ := json.Marshal(errs) fmt.Println(string(errsJson)) - fmt.Println(sender) } diff --git a/num/base.go b/num/base.go index 46d241a..0c9ca7d 100644 --- a/num/base.go +++ b/num/base.go @@ -126,13 +126,7 @@ func (i *baseConfigurator[T]) When(whenFn func(ctx context.Context, value any) b if whenFn == nil { return i } - base := i.c.NewWithWhen(func(ctx context.Context, value any) bool { - v, ok := value.(**T) - if !ok { - return false - } - return whenFn(ctx, v) - }) + base := i.c.NewWithWhen(whenFn) return &baseConfigurator[T]{ c: base, field: i.field, diff --git a/shared/field.go b/shared/field.go index 935fbad..5af6fe0 100644 --- a/shared/field.go +++ b/shared/field.go @@ -79,6 +79,7 @@ func (i *FieldConfigurator[T]) NewWithWhen(whenFn func(ctx context.Context, valu } i.appendFn(fnWithEnabler) }, + mk: i.mk, } } diff --git a/str/base.go b/str/base.go index aaa11ac..3646f33 100644 --- a/str/base.go +++ b/str/base.go @@ -110,13 +110,7 @@ func (i *baseConfigurator[T]) When(whenFn func(ctx context.Context, value any) b if whenFn == nil { return i } - base := i.c.NewWithWhen(func(ctx context.Context, value any) bool { - v, ok := value.(**T) - if !ok { - return false - } - return whenFn(ctx, v) - }) + base := i.c.NewWithWhen(whenFn) return &baseConfigurator[T]{ c: base, field: i.field, diff --git a/uuid/base.go b/uuid/base.go index a65ea84..a925e2d 100644 --- a/uuid/base.go +++ b/uuid/base.go @@ -52,9 +52,7 @@ func (i *baseConfigurator) When(whenFn func(ctx context.Context, value any) bool if whenFn == nil { return i } - base := i.c.NewWithWhen(func(ctx context.Context, value any) bool { - return whenFn(ctx, value) - }) + base := i.c.NewWithWhen(whenFn) return &baseConfigurator{ c: base, field: i.field,