Skip to content
Merged
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
20 changes: 14 additions & 6 deletions examples/translation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() + " ",
Expand All @@ -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)
}
8 changes: 1 addition & 7 deletions num/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions shared/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (i *FieldConfigurator[T]) NewWithWhen(whenFn func(ctx context.Context, valu
}
i.appendFn(fnWithEnabler)
},
mk: i.mk,
}
}

Expand Down
8 changes: 1 addition & 7 deletions str/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions uuid/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading