Skip to content

Commit 964236c

Browse files
authored
feat: forbid contractors when maintenance/type is not contract (#207)
See publiccodeyml/publiccode.yml#213. Fix #206.
1 parent 9497768 commit 964236c

9 files changed

+361
-3
lines changed

parser_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,15 @@ func TestInvalidTestcasesV0(t *testing.T) {
497497
"maintenance_contractors_website_invalid.yml": ValidationResults{
498498
ValidationError{"maintenance.contractors[0].website", "website must be an HTTP URL", 0, 0}, // TODO: line number
499499
},
500+
"maintenance_contractors_when_type_is_community.yml": ValidationResults{
501+
ValidationError{"maintenance.contractors", "contractors must not be present unless \"type\" is \"contract\"", 46, 3},
502+
},
503+
"maintenance_contractors_when_type_is_internal.yml": ValidationResults{
504+
ValidationError{"maintenance.contractors", "contractors must not be present unless \"type\" is \"contract\"", 46, 3},
505+
},
506+
"maintenance_contractors_when_type_is_none.yml": ValidationResults{
507+
ValidationError{"maintenance.contractors", "contractors must not be present unless \"type\" is \"contract\"", 46, 3},
508+
},
500509

501510
// localisation
502511
"localisation_availableLanguages_missing.yml": ValidationResults{
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
publiccodeYmlVersion: "0.4"
2+
3+
name: Medusa
4+
url: "https://github.com/italia/developers.italia.it.git"
5+
softwareVersion: "dev"
6+
releaseDate: "2017-04-15"
7+
8+
platforms:
9+
- web
10+
11+
categories:
12+
- cloud-management
13+
14+
developmentStatus: development
15+
16+
softwareType: "standalone/other"
17+
18+
description:
19+
en:
20+
localisedName: Medusa
21+
shortDescription: >
22+
A rather short description which
23+
is probably useless
24+
longDescription: >
25+
Very long description of this software, also split
26+
on multiple rows. You should note what the software
27+
is and why one should need it. This is 158 characters.
28+
Very long description of this software, also split
29+
on multiple rows. You should note what the software
30+
is and why one should need it. This is 316 characters.
31+
Very long description of this software, also split
32+
on multiple rows. You should note what the software
33+
is and why one should need it. This is 474 characters.
34+
Very long description of this software, also split
35+
on multiple rows. You should note what the software
36+
is and why one should need it. This is 632 characters.
37+
features:
38+
- Just one feature
39+
40+
legal:
41+
license: AGPL-3.0-or-later
42+
43+
maintenance:
44+
type: "community"
45+
46+
contractors:
47+
# This should NOT be accepted NOR validated (the "until" is missing),
48+
# because type is "community"
49+
- name: "Contractor"
50+
website: "https://example.org"
51+
52+
contacts:
53+
- name: John Smith
54+
55+
localisation:
56+
localisationReady: true
57+
availableLanguages:
58+
- en
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
publiccodeYmlVersion: "0.4"
2+
3+
name: Medusa
4+
url: "https://github.com/italia/developers.italia.it.git"
5+
softwareVersion: "dev"
6+
releaseDate: "2017-04-15"
7+
8+
platforms:
9+
- web
10+
11+
categories:
12+
- cloud-management
13+
14+
developmentStatus: development
15+
16+
softwareType: "standalone/other"
17+
18+
description:
19+
en:
20+
localisedName: Medusa
21+
shortDescription: >
22+
A rather short description which
23+
is probably useless
24+
longDescription: >
25+
Very long description of this software, also split
26+
on multiple rows. You should note what the software
27+
is and why one should need it. This is 158 characters.
28+
Very long description of this software, also split
29+
on multiple rows. You should note what the software
30+
is and why one should need it. This is 316 characters.
31+
Very long description of this software, also split
32+
on multiple rows. You should note what the software
33+
is and why one should need it. This is 474 characters.
34+
Very long description of this software, also split
35+
on multiple rows. You should note what the software
36+
is and why one should need it. This is 632 characters.
37+
features:
38+
- Just one feature
39+
40+
legal:
41+
license: AGPL-3.0-or-later
42+
43+
maintenance:
44+
type: "internal"
45+
46+
contractors:
47+
# This should NOT be accepted NOR validated (the "until" is missing),
48+
# because type is "internal"
49+
- name: "Contractor"
50+
website: "https://example.org"
51+
52+
contacts:
53+
- name: John Smith
54+
55+
localisation:
56+
localisationReady: true
57+
availableLanguages:
58+
- en
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
publiccodeYmlVersion: "0.4"
2+
3+
name: Medusa
4+
url: "https://github.com/italia/developers.italia.it.git"
5+
softwareVersion: "dev"
6+
releaseDate: "2017-04-15"
7+
8+
platforms:
9+
- web
10+
11+
categories:
12+
- cloud-management
13+
14+
developmentStatus: development
15+
16+
softwareType: "standalone/other"
17+
18+
description:
19+
en:
20+
localisedName: Medusa
21+
shortDescription: >
22+
A rather short description which
23+
is probably useless
24+
longDescription: >
25+
Very long description of this software, also split
26+
on multiple rows. You should note what the software
27+
is and why one should need it. This is 158 characters.
28+
Very long description of this software, also split
29+
on multiple rows. You should note what the software
30+
is and why one should need it. This is 316 characters.
31+
Very long description of this software, also split
32+
on multiple rows. You should note what the software
33+
is and why one should need it. This is 474 characters.
34+
Very long description of this software, also split
35+
on multiple rows. You should note what the software
36+
is and why one should need it. This is 632 characters.
37+
features:
38+
- Just one feature
39+
40+
legal:
41+
license: AGPL-3.0-or-later
42+
43+
maintenance:
44+
type: "none"
45+
46+
contractors:
47+
# This should NOT be accepted NOR validated (the "until" is missing),
48+
# because type is "none"
49+
- name: "Contractor"
50+
website: "https://example.org"
51+
52+
localisation:
53+
localisationReady: true
54+
availableLanguages:
55+
- en
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
publiccodeYmlVersion: "0.4"
2+
3+
name: Medusa
4+
url: "https://github.com/italia/developers.italia.it.git"
5+
6+
platforms:
7+
- web
8+
9+
categories:
10+
- cloud-management
11+
12+
developmentStatus: development
13+
14+
softwareType: "standalone/other"
15+
16+
description:
17+
en_GB:
18+
localisedName: Medusa
19+
shortDescription: >
20+
A rather short description which
21+
is probably useless
22+
longDescription: >
23+
Very long description of this software, also split
24+
on multiple rows. You should note what the software
25+
is and why one should need it. This is 158 characters.
26+
Very long description of this software, also split
27+
on multiple rows. You should note what the software
28+
is and why one should need it. This is 316 characters.
29+
Very long description of this software, also split
30+
on multiple rows. You should note what the software
31+
is and why one should need it. This is 474 characters.
32+
Very long description of this software, also split
33+
on multiple rows. You should note what the software
34+
is and why one should need it. This is 632 characters.
35+
features:
36+
- Just one feature
37+
38+
legal:
39+
license: AGPL-3.0-or-later
40+
41+
maintenance:
42+
type: "contract"
43+
44+
contractors:
45+
- name: "Contractor"
46+
website: "https://example.org"
47+
until: 2040-01-01
48+
49+
# `contacts` is allowed when type is "contract" as well
50+
contacts:
51+
- name: John Smith
52+
53+
localisation:
54+
localisationReady: true
55+
availableLanguages:
56+
- en
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
publiccodeYmlVersion: "0.4"
2+
3+
name: Medusa
4+
url: "https://github.com/italia/developers.italia.it.git"
5+
6+
platforms:
7+
- web
8+
9+
categories:
10+
- cloud-management
11+
12+
developmentStatus: development
13+
14+
softwareType: "standalone/other"
15+
16+
description:
17+
en_GB:
18+
localisedName: Medusa
19+
shortDescription: >
20+
A rather short description which
21+
is probably useless
22+
longDescription: >
23+
Very long description of this software, also split
24+
on multiple rows. You should note what the software
25+
is and why one should need it. This is 158 characters.
26+
Very long description of this software, also split
27+
on multiple rows. You should note what the software
28+
is and why one should need it. This is 316 characters.
29+
Very long description of this software, also split
30+
on multiple rows. You should note what the software
31+
is and why one should need it. This is 474 characters.
32+
Very long description of this software, also split
33+
on multiple rows. You should note what the software
34+
is and why one should need it. This is 632 characters.
35+
features:
36+
- Just one feature
37+
38+
legal:
39+
license: AGPL-3.0-or-later
40+
41+
maintenance:
42+
type: "internal"
43+
44+
# `contacts` is allowed when type is "internal" as well
45+
contacts:
46+
- name: John Smith
47+
48+
localisation:
49+
localisationReady: true
50+
availableLanguages:
51+
- en
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
publiccodeYmlVersion: "0.4"
2+
3+
name: Medusa
4+
url: "https://github.com/italia/developers.italia.it.git"
5+
6+
platforms:
7+
- web
8+
9+
categories:
10+
- cloud-management
11+
12+
developmentStatus: development
13+
14+
softwareType: "standalone/other"
15+
16+
description:
17+
en_GB:
18+
localisedName: Medusa
19+
shortDescription: >
20+
A rather short description which
21+
is probably useless
22+
longDescription: >
23+
Very long description of this software, also split
24+
on multiple rows. You should note what the software
25+
is and why one should need it. This is 158 characters.
26+
Very long description of this software, also split
27+
on multiple rows. You should note what the software
28+
is and why one should need it. This is 316 characters.
29+
Very long description of this software, also split
30+
on multiple rows. You should note what the software
31+
is and why one should need it. This is 474 characters.
32+
Very long description of this software, also split
33+
on multiple rows. You should note what the software
34+
is and why one should need it. This is 632 characters.
35+
features:
36+
- Just one feature
37+
38+
legal:
39+
license: AGPL-3.0-or-later
40+
41+
maintenance:
42+
type: "none"
43+
44+
# `contacts` is allowed when type is "none" as well
45+
contacts:
46+
- name: John Smith
47+
48+
localisation:
49+
localisationReady: true
50+
availableLanguages:
51+
- en

v0.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ type PublicCodeV0 struct {
5151
} `yaml:"legal"`
5252

5353
Maintenance struct {
54-
Type string `validate:"required,oneof=internal contract community none" yaml:"type"`
55-
Contractors []ContractorV0 `validate:"required_if=Type contract,dive" yaml:"contractors,omitempty"`
56-
Contacts []ContactV0 `validate:"required_if=Type community,required_if=Type internal,dive" yaml:"contacts,omitempty"`
54+
Type string `validate:"required,oneof=internal contract community none" yaml:"type"`
55+
Contractors []ContractorV0 `validate:"required_if=Type contract,excluded_unless=Type contract,dive" yaml:"contractors,omitempty"`
56+
Contacts []ContactV0 `validate:"required_if=Type community,required_if=Type internal,dive" yaml:"contacts,omitempty"`
5757
} `yaml:"maintenance"`
5858

5959
Localisation struct {

validators/validator.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,26 @@ func RegisterLocalErrorMessages(v *validator.Validate, trans ut.Translator) erro
102102
tag: "is_mime_type",
103103
translation: "{0} is not a valid MIME type",
104104
},
105+
{
106+
// Override the default error with a more user friendly one
107+
//
108+
// original:
109+
// foo is an excluded field
110+
// overridden:
111+
// foo is not permitted when "bar" is "foobar"
112+
tag: "excluded_unless",
113+
customRegisFunc: func(ut ut.Translator) error {
114+
return ut.Add("excluded_unless", "{0} must not be present unless {1}", true)
115+
},
116+
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
117+
parts := strings.Fields(fe.Param())
118+
119+
t, _ := ut.T("excluded_unless", fe.Field(), fmt.Sprintf("\"%s\" is \"%s\"", strings.ToLower(parts[0]), parts[1]))
120+
121+
return t
122+
},
123+
override: true,
124+
},
105125
{
106126
tag: "umax",
107127
customRegisFunc: func(ut ut.Translator) error {

0 commit comments

Comments
 (0)