diff --git a/internal/dtos/request/user.go b/internal/dtos/request/user.go index 282d7a2..b3e19c3 100644 --- a/internal/dtos/request/user.go +++ b/internal/dtos/request/user.go @@ -5,7 +5,7 @@ import "time" type UpdateProfileDto struct { DisplayName string `json:"display_name" validate:"omitempty,min=2,max=50"` FullName string `json:"full_name" validate:"omitempty,min=2,max=100"` - AvatarUrl string `json:"avatar_url" validate:"omitempty,url"` + AvatarUrl string `json:"avatar_url" validate:"omitempty,url,image_url"` Bio string `json:"bio" validate:"omitempty,max=255"` Location string `json:"location" validate:"omitempty,max=100"` Website string `json:"website" validate:"omitempty,url"` diff --git a/pkg/validator/validator.go b/pkg/validator/validator.go index ecfeb48..d5f63b9 100644 --- a/pkg/validator/validator.go +++ b/pkg/validator/validator.go @@ -2,6 +2,8 @@ package validator import ( "errors" + "net/url" + "path" "reflect" "strings" @@ -22,6 +24,31 @@ func init() { } return name }) + + validate.RegisterValidation("image_url", func(fl validator.FieldLevel) bool { + val := fl.Field().String() + if val == "" { + return true + } + return isImageURL(val) + }) + +} + +func isImageURL(u string) bool { + parsed, err := url.Parse(u) + if err != nil { + return false + } + + ext := strings.ToLower(path.Ext(parsed.Path)) + + switch ext { + case ".jpg", ".jpeg", ".png", ".gif", ".webp": + return true + default: + return false + } } type ErrorResponse struct { diff --git a/test.ts b/test.ts new file mode 100644 index 0000000..725fdcb --- /dev/null +++ b/test.ts @@ -0,0 +1,2 @@ +import axios from "axios" +