Skip to content

Commit

Permalink
Package AWS:
Browse files Browse the repository at this point in the history
- fix double import with different tag for validator in config model
- applys MR about fix on bucket validator to config AWS model
  • Loading branch information
nabbar committed May 30, 2024
1 parent 88421f1 commit 39df399
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 18 additions & 2 deletions aws/configAws/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"fmt"
"net"
"net/url"
"regexp"

sdkaws "github.com/aws/aws-sdk-go-v2/aws"
libval "github.com/go-playground/validator/v10"
Expand All @@ -41,7 +42,7 @@ type Model struct {
Region string `mapstructure:"region" json:"region" yaml:"region" toml:"region" validate:"printascii,required"`
AccessKey string `mapstructure:"accesskey" json:"accesskey" yaml:"accesskey" toml:"accesskey" validate:"printascii,required"`
SecretKey string `mapstructure:"secretkey" json:"secretkey" yaml:"secretkey" toml:"secretkey" validate:"printascii,required"`
Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket" toml:"bucket" validate:"printascii,omitempty"`
Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket" toml:"bucket" validate:"printascii,omitempty,bucket-s3"`
}

type ModelStatus struct {
Expand All @@ -54,10 +55,25 @@ type awsModel struct {
retryer func() sdkaws.Retryer
}

func validateBucketS3(fl libval.FieldLevel) bool {
value := fl.Field().String()
re := regexp.MustCompile(`^[A-Za-z0-9]([A-Za-z0-9]|\.[A-Za-z0-9]|-[A-Za-z0-9]){0,46}[A-Za-z0-9]$`)
if !re.MatchString(value) {
return false
}
return true
}

func (c *awsModel) Validate() error {
err := ErrorConfigValidator.Error(nil)

if er := libval.New().Struct(c); er != nil {
validate := libval.New()
valErr := validate.RegisterValidation("bucket-s3", validateBucketS3)
if valErr != nil {
err.Add(valErr)
}

if er := validate.Struct(c); er != nil {
if e, ok := er.(*libval.InvalidValidationError); ok {
err.Add(e)
}
Expand Down
4 changes: 1 addition & 3 deletions aws/configCustom/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ import (
"regexp"
"strings"

"github.com/go-playground/validator/v10"

sdkaws "github.com/aws/aws-sdk-go-v2/aws"
libval "github.com/go-playground/validator/v10"
libhtc "github.com/nabbar/golib/httpcli"
Expand Down Expand Up @@ -62,7 +60,7 @@ type awsModel struct {
mapRegion map[string]*url.URL
}

func validateBucketS3(fl validator.FieldLevel) bool {
func validateBucketS3(fl libval.FieldLevel) bool {
value := fl.Field().String()
re := regexp.MustCompile(`^[A-Za-z0-9]([A-Za-z0-9]|\.[A-Za-z0-9]|-[A-Za-z0-9]){0,46}[A-Za-z0-9]$`)
if !re.MatchString(value) {
Expand Down

0 comments on commit 39df399

Please sign in to comment.