Skip to content

Commit 634be2c

Browse files
committed
feat: forbid contractors when maintenance/type is not contract
See publiccodeyml/publiccode.yml#213. Fix italia#206.
1 parent de09535 commit 634be2c

9 files changed

+359
-1
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type PublicCodeV0 struct {
5252

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

validators/validator.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,26 @@ func RegisterLocalErrorMessages(v *validator.Validate, trans ut.Translator) erro
9595
},
9696
override: true,
9797
},
98+
{
99+
// Override the default error with a more user friendly one
100+
//
101+
// original:
102+
// foo is an excluded field
103+
// overridden:
104+
// foo is not permitted when "bar" is "foobar"
105+
tag: "required_if",
106+
customRegisFunc: func(ut ut.Translator) error {
107+
return ut.Add("excluded_unless", "{0} must not be present unless {1}", true)
108+
},
109+
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
110+
parts := strings.Fields(fe.Param())
111+
112+
t, _ := ut.T("excluded_unless", fe.Field(), fmt.Sprintf("\"%s\" is \"%s\"", strings.ToLower(parts[0]), parts[1]))
113+
114+
return t
115+
},
116+
override: true,
117+
},
98118
{
99119
tag: "umax",
100120
customRegisFunc: func(ut ut.Translator) error {

0 commit comments

Comments
 (0)