From 39df39938eec4a0913fd03af0722e8bd4fe77646 Mon Sep 17 00:00:00 2001 From: Nicolas JUHEL Date: Thu, 30 May 2024 16:34:02 +0200 Subject: [PATCH] Package AWS: - fix double import with different tag for validator in config model - applys MR about fix on bucket validator to config AWS model --- aws/configAws/models.go | 20 ++++++++++++++++++-- aws/configCustom/models.go | 4 +--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/aws/configAws/models.go b/aws/configAws/models.go index c9445321..189feb16 100644 --- a/aws/configAws/models.go +++ b/aws/configAws/models.go @@ -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" @@ -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 { @@ -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) } diff --git a/aws/configCustom/models.go b/aws/configCustom/models.go index 67eb7287..c42f5880 100644 --- a/aws/configCustom/models.go +++ b/aws/configCustom/models.go @@ -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" @@ -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) {