From 0d559ebc5ad0e3516bbd34a53976262cd8acdf59 Mon Sep 17 00:00:00 2001 From: Fabio Bonelli Date: Thu, 8 Feb 2024 19:49:32 +0100 Subject: [PATCH] refactor: validate mime types via the validation library --- check_generics.go | 10 ---------- keys.go | 16 +--------------- parser.go | 1 + publiccode.go | 4 ++-- validators/common.go | 9 +++++++++ validators/validator.go | 1 + 6 files changed, 14 insertions(+), 27 deletions(-) diff --git a/check_generics.go b/check_generics.go index 9d0f3fd..d5573ea 100644 --- a/check_generics.go +++ b/check_generics.go @@ -10,7 +10,6 @@ import ( "os" "path" "path/filepath" - "regexp" "runtime" "strings" @@ -194,12 +193,3 @@ func (p *Parser) validLogo(u url.URL) (bool, error) { return true, nil } - -// isMIME checks whether the string in input is a well formed MIME or not. -func (p *Parser) isMIME(value string) bool { - // Regex for MIME. - // Reference: https://github.com/jshttp/media-typer/ - re := regexp.MustCompile("^ *([A-Za-z0-9][A-Za-z0-9!#$&^_-]{0,126})/([A-Za-z0-9][A-Za-z0-9!#$&^_.+-]{0,126}) *$") - - return re.MatchString(value) -} diff --git a/keys.go b/keys.go index 373cd8d..ba3b8ee 100644 --- a/keys.go +++ b/keys.go @@ -86,24 +86,10 @@ func (p *Parser) validateFields() error { if len(p.PublicCode.InputTypes) > 0 { vr = append(vr, ValidationWarning{"inputTypes", "This key is DEPRECATED and will be removed in the future", 0, 0}) } - for i, mimeType := range p.PublicCode.InputTypes { - if !p.isMIME(mimeType) { - vr = append(vr, newValidationError( - fmt.Sprintf("inputTypes[%d]", i), "'%s' is not a MIME type", mimeType, - )) - } - } - if len(p.PublicCode.OutputTypes) > 0 { vr = append(vr, ValidationWarning{"outputTypes", "This key is DEPRECATED and will be removed in the future", 0, 0}) } - for i, mimeType := range p.PublicCode.OutputTypes { - if !p.isMIME(mimeType) { - vr = append(vr, newValidationError( - fmt.Sprintf("outputTypes[%d]", i), "'%s' is not a MIME type", mimeType, - )) - } - } + for lang, desc := range p.PublicCode.Description { if p.PublicCode.Description == nil { diff --git a/parser.go b/parser.go index b72086d..ef6b048 100644 --- a/parser.go +++ b/parser.go @@ -238,6 +238,7 @@ func (p *Parser) ParseBytes(in []byte) error { "oneof": "must be one of the following:", "email": "must be a valid email", "date": "must be a date with format 'YYYY-MM-DD'", + "is_mime_type": "is not a MIME type", "umax": "must be less or equal than", "umin": "must be more or equal than", "is_category_v0_2": "must be a valid category", diff --git a/publiccode.go b/publiccode.go index 6cda4be..bcef259 100644 --- a/publiccode.go +++ b/publiccode.go @@ -23,8 +23,8 @@ type PublicCode struct { Logo string `yaml:"logo,omitempty"` MonochromeLogo string `yaml:"monochromeLogo,omitempty"` - InputTypes []string `yaml:"inputTypes,omitempty"` - OutputTypes []string `yaml:"outputTypes,omitempty"` + InputTypes []string `yaml:"inputTypes,omitempty" validate:"omitempty,dive,is_mime_type"` + OutputTypes []string `yaml:"outputTypes,omitempty" validate:"omitempty,dive,is_mime_type"` Platforms []string `yaml:"platforms" validate:"gt=0"` diff --git a/validators/common.go b/validators/common.go index f6b8ae8..5164375 100644 --- a/validators/common.go +++ b/validators/common.go @@ -1,6 +1,7 @@ package validators; import ( + "regexp" "strconv" "time" @@ -40,3 +41,11 @@ func uMin(fl validator.FieldLevel) bool { return length >= min } + +// isMIMEType checks whether the string in input is a well formed MIME type or not. +func isMIMEType(fl validator.FieldLevel) bool { + // Reference: https://github.com/jshttp/media-typer/ + re := regexp.MustCompile("^ *([A-Za-z0-9][A-Za-z0-9!#$&^_-]{0,126})/([A-Za-z0-9][A-Za-z0-9!#$&^_.+-]{0,126}) *$") + + return re.MatchString(fl.Field().String()) +} diff --git a/validators/validator.go b/validators/validator.go index 853a51a..71698c3 100644 --- a/validators/validator.go +++ b/validators/validator.go @@ -10,6 +10,7 @@ import ( func New() *validator.Validate { validate := validator.New() _ = validate.RegisterValidation("date", isDate) + _ = validate.RegisterValidation("is_mime_type", isMIMEType) _ = validate.RegisterValidation("iso3166_1_alpha2_lowercase", isIso3166Alpha2Lowercase) _ = validate.RegisterValidation("umax", uMax) _ = validate.RegisterValidation("umin", uMin)