Skip to content

Commit 038716d

Browse files
refactor(validation): rename validation error message map for clarity and improve error handling during registration
1 parent 68a3cef commit 038716d

4 files changed

Lines changed: 14 additions & 10 deletions

File tree

pkg/api/api.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ type Service struct {
214214

215215
whitelistedWithdrawalAddress []common.Address
216216

217-
preMapHooks map[string]func(v string) (string, error)
218-
validationCustomErrorMessages map[string]func(err validator.FieldError) error
219-
validate *validator.Validate
217+
preMapHooks map[string]func(v string) (string, error)
218+
customValidationMessages map[string]func(err validator.FieldError) error
219+
validate *validator.Validate
220220

221221
redistributionAgent *storageincentives.Agent
222222

@@ -712,7 +712,7 @@ func (s *Service) mapStructure(input, output any) func(string, log.Logger, http.
712712
val = string(v)
713713
}
714714
var cause error
715-
if msgFn, ok := s.validationCustomErrorMessages[err.Tag()]; ok {
715+
if msgFn, ok := s.customValidationMessages[err.Tag()]; ok {
716716
cause = msgFn(err)
717717
} else {
718718
cause = fmt.Errorf("want %s:%s", err.Tag(), err.Param())

pkg/api/bytes_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ func TestBytesRedundancyLevel(t *testing.T) {
449449
Reasons: []jsonhttp.Reason{
450450
{
451451
Field: "swarm-redundancy-level",
452-
Error: fmt.Sprintf("want redundancy level to be in the range of %d and %d", int(redundancy.NONE), int(redundancy.PARANOID)),
452+
Error: fmt.Sprintf("want redundancy level to be between %d and %d", int(redundancy.NONE), int(redundancy.PARANOID)),
453453
},
454454
},
455455
},

pkg/api/bzz_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,7 @@ func TestBzzUploadRedundancyLevel(t *testing.T) {
12241224
Reasons: []jsonhttp.Reason{
12251225
{
12261226
Field: "swarm-redundancy-level",
1227-
Error: fmt.Sprintf("want lte:%d", maxValidLevel),
1227+
Error: fmt.Sprintf("want redundancy level to be between %d and %d", int(redundancy.NONE), int(redundancy.PARANOID)),
12281228
},
12291229
},
12301230
},
@@ -1300,7 +1300,7 @@ func TestBzzDownloadRedundancyLevel(t *testing.T) {
13001300
Reasons: []jsonhttp.Reason{
13011301
{
13021302
Field: "swarm-redundancy-level",
1303-
Error: fmt.Sprintf("want redundancy level to be in the range of %d and %d", int(redundancy.NONE), int(redundancy.PARANOID)),
1303+
Error: fmt.Sprintf("want redundancy level to be between %d and %d", int(redundancy.NONE), int(redundancy.PARANOID)),
13041304
},
13051305
},
13061306
},

pkg/api/validation.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ const (
1717

1818
// setupValidation configures custom validation rules and their custom error messages.
1919
func (s *Service) setupValidation() {
20-
s.validate.RegisterValidation(RedundancyLevelTag, func(fl validator.FieldLevel) bool {
20+
err := s.validate.RegisterValidation(RedundancyLevelTag, func(fl validator.FieldLevel) bool {
2121
level := redundancy.Level(fl.Field().Uint())
2222
return level.Validate()
2323
})
24+
if err != nil {
25+
s.logger.Error(err, "failed to register validation")
26+
panic(err)
27+
}
2428

25-
s.validationCustomErrorMessages = map[string]func(err validator.FieldError) error{
29+
s.customValidationMessages = map[string]func(err validator.FieldError) error{
2630
RedundancyLevelTag: func(err validator.FieldError) error {
27-
return fmt.Errorf("want redundancy level to be in the range of %d and %d", int(redundancy.NONE), int(redundancy.PARANOID))
31+
return fmt.Errorf("want redundancy level to be between %d and %d", int(redundancy.NONE), int(redundancy.PARANOID))
2832
},
2933
}
3034
}

0 commit comments

Comments
 (0)