Skip to content

Commit dd70a56

Browse files
authored
refactor: validate mime types via the validation library (italia#172)
1 parent 25be317 commit dd70a56

File tree

7 files changed

+81
-30
lines changed

7 files changed

+81
-30
lines changed

fields.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,10 @@ func validateFieldsV0(publiccode PublicCode, parser Parser, network bool) error
7474
vr = append(vr, ValidationWarning{"inputTypes", "This key is DEPRECATED and will be removed in the future", 0, 0})
7575
}
7676

77-
for i, mimeType := range publiccodev0.InputTypes {
78-
if !isMIME(mimeType) {
79-
vr = append(vr, newValidationError(
80-
fmt.Sprintf("inputTypes[%d]", i), "'%s' is not a MIME type", mimeType,
81-
))
82-
}
83-
}
84-
8577
if len(publiccodev0.OutputTypes) > 0 {
8678
vr = append(vr, ValidationWarning{"outputTypes", "This key is DEPRECATED and will be removed in the future", 0, 0})
8779
}
8880

89-
for i, mimeType := range publiccodev0.OutputTypes {
90-
if !isMIME(mimeType) {
91-
vr = append(vr, newValidationError(
92-
fmt.Sprintf("outputTypes[%d]", i), "'%s' is not a MIME type", mimeType,
93-
))
94-
}
95-
}
96-
9781
for lang, desc := range publiccodev0.Description {
9882
if publiccodev0.Description == nil {
9983
publiccodev0.Description = make(map[string]DescV0)

parser_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,17 @@ func TestInvalidTestcasesV0(t *testing.T) {
256256

257257
// inputTypes
258258
"inputTypes_invalid.yml": ValidationResults{
259+
ValidationError{"inputTypes[1]", "inputTypes[1] is not a valid MIME type", 1, 1},
259260
ValidationWarning{"inputTypes", "This key is DEPRECATED and will be removed in the future", 14, 1},
260-
ValidationError{"inputTypes[1]", "'foobar' is not a MIME type", 1, 1},
261261
},
262262
"inputTypes_wrong_type.yml": ValidationResults{
263263
ValidationError{"inputTypes.foobar", "wrong type for this field", 15, 1},
264264
},
265265

266266
// outputTypes
267267
"outputTypes_invalid.yml": ValidationResults{
268+
ValidationError{"outputTypes[1]", "outputTypes[1] is not a valid MIME type", 1, 1},
268269
ValidationWarning{"outputTypes", "This key is DEPRECATED and will be removed in the future", 14, 1},
269-
ValidationError{"outputTypes[1]", "'foobar' is not a MIME type", 1, 1},
270270
},
271271
"outputTypes_wrong_type.yml": ValidationResults{
272272
ValidationError{"outputTypes.foobar", "wrong type for this field", 15, 1},
@@ -594,6 +594,10 @@ func TestValidWithWarningsTestcasesV0(t *testing.T) {
594594
"valid.minimal.v0.3.yml": ValidationResults{
595595
ValidationWarning{"publiccodeYmlVersion", "v0.3 is not the latest version, use '0.4.0'. Parsing this file as v0.4.0.", 1, 1},
596596
},
597+
"valid.mime_types.yml": ValidationResults{
598+
ValidationWarning{"inputTypes", "This key is DEPRECATED and will be removed in the future", 48, 1},
599+
ValidationWarning{"outputTypes", "This key is DEPRECATED and will be removed in the future", 50, 1},
600+
},
597601
}
598602

599603
testFiles, _ := filepath.Glob("testdata/v0/valid_with_warnings/*yml")
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
publiccodeYmlVersion: "0.4"
2+
3+
name: Medusa
4+
url: "https://github.com/italia/developers.italia.it.git"
5+
releaseDate: "2017-04-15"
6+
7+
platforms:
8+
- web
9+
10+
categories:
11+
- cloud-management
12+
13+
developmentStatus: development
14+
15+
softwareType: "standalone/other"
16+
17+
description:
18+
en:
19+
localisedName: Medusa
20+
shortDescription: >
21+
A rather short description which
22+
is probably useless
23+
longDescription: >
24+
Very long description of this software, also split
25+
on multiple rows. You should note what the software
26+
is and why one should need it. This is 158 characters.
27+
Very long description of this software, also split
28+
on multiple rows. You should note what the software
29+
is and why one should need it. This is 316 characters.
30+
Very long description of this software, also split
31+
on multiple rows. You should note what the software
32+
is and why one should need it. This is 474 characters.
33+
Very long description of this software, also split
34+
on multiple rows. You should note what the software
35+
is and why one should need it. This is 632 characters.
36+
features:
37+
- Just one feature
38+
39+
legal:
40+
license: AGPL-3.0-or-later
41+
42+
maintenance:
43+
type: "community"
44+
45+
contacts:
46+
- name: Francesco Rossi
47+
48+
inputTypes:
49+
- application/json
50+
outputTypes:
51+
- image/svg+xml
52+
- audio/ogg
53+
- application/x-yaml
54+
- image/jpeg
55+
56+
localisation:
57+
localisationReady: true
58+
availableLanguages:
59+
- en

v0.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ type PublicCodeV0 struct {
2020
Logo string `yaml:"logo,omitempty"`
2121
MonochromeLogo string `yaml:"monochromeLogo,omitempty"`
2222

23-
InputTypes []string `yaml:"inputTypes,omitempty"`
24-
OutputTypes []string `yaml:"outputTypes,omitempty"`
23+
InputTypes []string `validate:"omitempty,dive,is_mime_type" yaml:"inputTypes,omitempty"`
24+
OutputTypes []string `validate:"omitempty,dive,is_mime_type" yaml:"outputTypes,omitempty"`
2525

2626
Platforms []string `validate:"gt=0" yaml:"platforms"`
2727

validations.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"os"
1212
"path"
1313
"path/filepath"
14-
"regexp"
1514
"runtime"
1615
"slices"
1716
"strings"
@@ -216,15 +215,6 @@ func (p *Parser) validLogo(u url.URL, network bool) (bool, error) {
216215
return true, nil
217216
}
218217

219-
// isMIME checks whether the string in input is a well formed MIME or not.
220-
func isMIME(value string) bool {
221-
// Regex for MIME.
222-
// Reference: https://github.com/jshttp/media-typer/
223-
re := regexp.MustCompile("^ *([A-Za-z0-9][A-Za-z0-9!#$&^_-]{0,126})/([A-Za-z0-9][A-Za-z0-9!#$&^_.+-]{0,126}) *$")
224-
225-
return re.MatchString(value)
226-
}
227-
228218
// isOembedURL returns whether the link is from a valid oEmbed provider.
229219
// Reference: https://oembed.com/providers.json
230220
func (p *Parser) isOEmbedURL(url *url.URL) (bool, error) {

validators/common.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package validators
33
import (
44
"fmt"
55
"reflect"
6+
"regexp"
67
"strconv"
78
"strings"
89
"time"
@@ -96,3 +97,11 @@ func isSPDXExpression(fl validator.FieldLevel) bool {
9697

9798
return valid
9899
}
100+
101+
// isMIMEType checks whether the string in input is a well formed MIME type or not.
102+
func isMIMEType(fl validator.FieldLevel) bool {
103+
// Reference: https://github.com/jshttp/media-typer/
104+
re := regexp.MustCompile("^ *([A-Za-z0-9][A-Za-z0-9!#$&^_-]{0,126})/([A-Za-z0-9][A-Za-z0-9!#$&^_.+-]{0,126}) *$")
105+
106+
return re.MatchString(fl.Field().String())
107+
}

validators/validator.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
func New() *validator.Validate {
1414
validate := validator.New(validator.WithRequiredStructEnabled())
1515
_ = validate.RegisterValidation("date", isDate)
16+
_ = validate.RegisterValidation("is_mime_type", isMIMEType)
1617
_ = validate.RegisterValidation("iso3166_1_alpha2_lowercase", isIso3166Alpha2Lowercase)
1718
_ = validate.RegisterValidation("umax", uMax)
1819
_ = validate.RegisterValidation("umin", uMin)
@@ -97,6 +98,10 @@ func RegisterLocalErrorMessages(v *validator.Validate, trans ut.Translator) erro
9798
},
9899
override: true,
99100
},
101+
{
102+
tag: "is_mime_type",
103+
translation: "{0} is not a valid MIME type",
104+
},
100105
{
101106
tag: "umax",
102107
customRegisFunc: func(ut ut.Translator) error {

0 commit comments

Comments
 (0)