Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: validate mime types via the validation library #172

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions check_generics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"

Expand Down Expand Up @@ -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)
}
16 changes: 1 addition & 15 deletions keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions publiccode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
9 changes: 9 additions & 0 deletions validators/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validators;

import (
"regexp"
"strconv"
"time"

Expand Down Expand Up @@ -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())
}
1 change: 1 addition & 0 deletions validators/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading