Skip to content

Commit

Permalink
improve(api,pkg): set validation rules to it own type
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybarreto authored and gustavosbarreto committed Dec 10, 2023
1 parent 6b72a8a commit c01e294
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion api/services/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
30 changes: 22 additions & 8 deletions pkg/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -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"),
},
}

Expand All @@ -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))
}

Expand Down

0 comments on commit c01e294

Please sign in to comment.