From c01e294e0c12abd97bc07fe4546d25e7627d7b21 Mon Sep 17 00:00:00 2001 From: Henry Barreto Date: Tue, 5 Dec 2023 15:01:03 -0300 Subject: [PATCH] improve(api,pkg): set validation rules to it own type --- api/services/device.go | 2 +- pkg/validator/validator.go | 30 ++++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/api/services/device.go b/api/services/device.go index 6cdc5480645..d56e60bd21f 100644 --- a/api/services/device.go +++ b/api/services/device.go @@ -351,7 +351,7 @@ func (s *service) UpdateDevice(ctx context.Context, tenant string, uid models.UI } v := validator.New() - if ok, err := v.Var(*name, "device_name"); err != nil || !ok { + if ok, err := v.Var(*name, validator.DeviceNameTag); err != nil || !ok { return NewErrDeviceInvalid(map[string]interface{}{"name": *name}, nil) } diff --git a/pkg/validator/validator.go b/pkg/validator/validator.go index e70c9db7250..bdb6cdce3e9 100644 --- a/pkg/validator/validator.go +++ b/pkg/validator/validator.go @@ -14,10 +14,24 @@ type Rule struct { Error error } +// Tag is the rule used to validate a variable or a structure's field. +type Tag string + +const ( + // RegexpTag indicates that the regexp must be valide. + RegexpTag = "regexp" + // UserNameTag contains the rule to validate the user's name. + UserNameTag = "username" + // UserPasswordTag contains the rule to validate the user's password. + UserPasswordTag = "password" + // DeviceNameTag contains the rule to validate the device's name. + DeviceNameTag = "device_name" +) + // Rules is a slice that contains all validation rules. var Rules = []Rule{ { - Tag: "regexp", + Tag: RegexpTag, Handler: func(field validator.FieldLevel) bool { _, err := regexp.Compile(field.Field().String()) @@ -26,25 +40,25 @@ var Rules = []Rule{ Error: fmt.Errorf("the regexp is invalid"), }, { - Tag: "username", + Tag: UserNameTag, Handler: func(field validator.FieldLevel) bool { return regexp.MustCompile(`^([a-zA-Z0-9-_.@]){3,30}$`).MatchString(field.Field().String()) }, Error: fmt.Errorf("the username must be between 3 and 30 characters, and can only contain letters, numbers, and the following characters: -_.@"), }, { - Tag: "password", + Tag: UserPasswordTag, Handler: func(field validator.FieldLevel) bool { return regexp.MustCompile(`^(.){5,30}$`).MatchString(field.Field().String()) }, Error: fmt.Errorf("the password cannot be empty and must be between 5 and 30 characters"), }, { - Tag: "device_name", + Tag: DeviceNameTag, Handler: func(field validator.FieldLevel) bool { - return regexp.MustCompile(`^([a-zA-Z0-9_.-] ){1,64}$`).MatchString(field.Field().String()) + return regexp.MustCompile(`^([a-zA-Z0-9_-]){1,64}$`).MatchString(field.Field().String()) }, - Error: fmt.Errorf("the device name can only contain `_`, `.` and alpha numeric characters"), + Error: fmt.Errorf("the device name can only contain `_`, `-` and alpha numeric characters"), }, } @@ -70,8 +84,8 @@ func New() *Validator { } // Var validates a variable using a ShellHub validation's tags. -func (v *Validator) Var(value, tag string) (bool, error) { - if err := v.Validate.Var(value, tag); err != nil { +func (v *Validator) Var(value string, tag Tag) (bool, error) { + if err := v.Validate.Var(value, string(tag)); err != nil { return false, fmt.Errorf("invalid variable: %w", fmt.Errorf("invalid validation on value %s using tag %s", value, tag)) }