Package AWS : Validator tag for s3 bucket (#184)

Package AWS:
- fix validator tag for s3 bucket: use custom function instead of hostname
This commit is contained in:
Valentin Morel
2024-05-30 16:23:39 +02:00
committed by GitHub
parent dbeb385267
commit 88421f1aac

View File

@@ -30,8 +30,11 @@ import (
"fmt"
"net"
"net/url"
"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"
@@ -43,7 +46,7 @@ type Model struct {
Endpoint string `mapstructure:"endpoint" json:"endpoint" yaml:"endpoint" toml:"endpoint" validate:"url"`
AccessKey string `mapstructure:"accesskey" json:"accesskey" yaml:"accesskey" toml:"accesskey" validate:"omitempty,printascii"`
SecretKey string `mapstructure:"secretkey" json:"secretkey" yaml:"secretkey" toml:"secretkey" validate:"omitempty,printascii"`
Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket" toml:"bucket" validate:"omitempty,hostname"`
Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket" toml:"bucket" validate:"omitempty,bucket-s3"`
}
type ModelStatus struct {
@@ -59,10 +62,25 @@ type awsModel struct {
mapRegion map[string]*url.URL
}
func validateBucketS3(fl validator.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)
}