Skip to content

Commit 6b29485

Browse files
Yury MiadzeletsInsei
Yury Miadzelets
authored andcommitted
fix panic when using typed When
1 parent 9409d8f commit 6b29485

File tree

5 files changed

+18
-23
lines changed

5 files changed

+18
-23
lines changed

examples/translation/main.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,19 @@ func main() {
4848
allowedClientID := uuid.New()
4949
valigo.Configure[Sender](v, func(builder valigo.Configurator[Sender], obj *Sender) {
5050
builder.Number(&obj.Int).AnyOf(2)
51-
builder.String(&obj.Type).Trim()
51+
builder.String(&obj.Type).When(func(ctx context.Context, value any) bool {
52+
return true
53+
}).Trim()
54+
builder.String(&obj.Description).When(func(ctx context.Context, value any) bool {
55+
return true
56+
}).Trim()
5257
builder.String(&obj.SMTPHost).Trim().
5358
Regexp(regexp.MustCompile("^[a-zA-Z0-9.]+$"), str.WithRegexpLocaleKey(customRegexpLocaleKey))
54-
builder.UUID(&obj.Id).Required()
59+
builder.UUID(&obj.Id).When(func(ctx context.Context, value any) bool {
60+
return true
61+
}).AnyOf(uuid.Nil)
5562
builder.String(&obj.Email).Email()
5663
builder.StringSlice(&obj.Emails).Email()
57-
builder.String(&obj.Description).Trim()
5864
builder.StringSlice(&obj.Templates).Trim().
5965
Regexp(regexp.MustCompile("^[a-zA-Z0-9.]+$"), str.WithRegexpLocaleKey(customRegexpLocaleKey))
6066
builder.UUIDSlice(&obj.ClientIDs).AnyOf(allowedClientID)
@@ -63,8 +69,10 @@ func main() {
6369
if err != nil {
6470
panic(err)
6571
}
72+
desc := "desc "
6673
sender := &Sender{
67-
Type: "123@123 ",
74+
Type: "type ",
75+
Description: &desc,
6876
Templates: []string{" correct ", "incorrect&"},
6977
ClientIDs: []*uuid.UUID{&id},
7078
SMTPHost: uuid.New().String() + " ",
@@ -74,10 +82,10 @@ func main() {
7482
Email: "correct@email.com",
7583
Emails: []string{"correct@email.com", "incorrect.email.com"},
7684
Id: id,
77-
Int: 2,
85+
Int: 5,
7886
}
87+
7988
errs := v.Validate(context.Background(), sender)
8089
errsJson, _ := json.Marshal(errs)
8190
fmt.Println(string(errsJson))
82-
fmt.Println(sender)
8391
}

num/base.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,7 @@ func (i *baseConfigurator[T]) When(whenFn func(ctx context.Context, value any) b
126126
if whenFn == nil {
127127
return i
128128
}
129-
base := i.c.NewWithWhen(func(ctx context.Context, value any) bool {
130-
v, ok := value.(**T)
131-
if !ok {
132-
return false
133-
}
134-
return whenFn(ctx, v)
135-
})
129+
base := i.c.NewWithWhen(whenFn)
136130
return &baseConfigurator[T]{
137131
c: base,
138132
field: i.field,

shared/field.go

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func (i *FieldConfigurator[T]) NewWithWhen(whenFn func(ctx context.Context, valu
7979
}
8080
i.appendFn(fnWithEnabler)
8181
},
82+
mk: i.mk,
8283
}
8384
}
8485

str/base.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,7 @@ func (i *baseConfigurator[T]) When(whenFn func(ctx context.Context, value any) b
110110
if whenFn == nil {
111111
return i
112112
}
113-
base := i.c.NewWithWhen(func(ctx context.Context, value any) bool {
114-
v, ok := value.(**T)
115-
if !ok {
116-
return false
117-
}
118-
return whenFn(ctx, v)
119-
})
113+
base := i.c.NewWithWhen(whenFn)
120114
return &baseConfigurator[T]{
121115
c: base,
122116
field: i.field,

uuid/base.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ func (i *baseConfigurator) When(whenFn func(ctx context.Context, value any) bool
5252
if whenFn == nil {
5353
return i
5454
}
55-
base := i.c.NewWithWhen(func(ctx context.Context, value any) bool {
56-
return whenFn(ctx, value)
57-
})
55+
base := i.c.NewWithWhen(whenFn)
5856
return &baseConfigurator{
5957
c: base,
6058
field: i.field,

0 commit comments

Comments
 (0)