Skip to content

Commit 53e1064

Browse files
committed
feat: forbid contractors when maintenance/type is not contract
Fix italia#206.
1 parent b138ab8 commit 53e1064

9 files changed

+345
-5
lines changed

parser.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) {
193193
"iso3166_1_alpha2_lowercase": "must be a valid lowercase ISO 3166-1 alpha-2 two-letter country code",
194194
"bcp47_language_tag": "must be a valid BCP 47 language",
195195
"bcp47_keys": "must use a valid BCP 47 language",
196+
"required_if": "must be present if",
197+
"excluded_unless": "must not be present unless",
196198
}
197199
for _, err := range err.(validator.ValidationErrors) {
198200
var sb strings.Builder

parser_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,10 @@ func TestInvalidTestcasesV0(t *testing.T) {
464464
ValidationError{"maintenance.type", "must be one of the following: internal contract community none", 45, 3},
465465
},
466466
"maintenance_contacts_missing_with_type_community.yml": ValidationResults{
467-
ValidationError{"maintenance.contacts", "required_if Type community", 44, 3},
467+
ValidationError{"maintenance.contacts", "must be present if Type community", 44, 3},
468468
},
469469
"maintenance_contacts_missing_with_type_internal.yml": ValidationResults{
470-
ValidationError{"maintenance.contacts", "required_if Type internal", 44, 3},
470+
ValidationError{"maintenance.contacts", "must be present if Type internal", 44, 3},
471471
},
472472
"maintenance_contacts_name_missing.yml": ValidationResults{
473473
ValidationError{"maintenance.contacts[0].name", "required", 0, 0},
@@ -476,11 +476,11 @@ func TestInvalidTestcasesV0(t *testing.T) {
476476
ValidationError{"maintenance.contacts[0].email", "must be a valid email", 0, 0},
477477
},
478478
"maintenance_contractors_missing_with_type_contract.yml": ValidationResults{
479-
ValidationError{"maintenance.contractors", "required_if Type contract", 44, 3},
479+
ValidationError{"maintenance.contractors", "must be present if Type contract", 44, 3},
480480
},
481481
"maintenance_contractors_invalid_type.yml": ValidationResults{
482482
ValidationError{"maintenance.contractors", "wrong type for this field", 47, 1},
483-
ValidationError{"maintenance.contractors", "required_if Type contract", 47, 3},
483+
ValidationError{"maintenance.contractors", "must be present if Type contract", 47, 3},
484484
},
485485
"maintenance_contractors_name_missing.yml": ValidationResults{
486486
ValidationError{"maintenance.contractors[0].name", "required", 0, 0},
@@ -497,6 +497,15 @@ func TestInvalidTestcasesV0(t *testing.T) {
497497
"maintenance_contractors_website_invalid.yml": ValidationResults{
498498
ValidationError{"maintenance.contractors[0].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", "must not be present unless Type contract", 46, 3},
502+
},
503+
"maintenance_contractors_when_type_is_internal.yml": ValidationResults{
504+
ValidationError{"maintenance.contractors", "must not be present unless Type contract", 46, 3},
505+
},
506+
"maintenance_contractors_when_type_is_none.yml": ValidationResults{
507+
ValidationError{"maintenance.contractors", "must not be present unless Type 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

0 commit comments

Comments
 (0)