Skip to content

feat: forbid contractors when maintenance/type is not contract #207

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

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
9 changes: 9 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,15 @@ func TestInvalidTestcasesV0(t *testing.T) {
"maintenance_contractors_website_invalid.yml": ValidationResults{
ValidationError{"maintenance.contractors[0].website", "website must be an HTTP URL", 0, 0}, // TODO: line number
},
"maintenance_contractors_when_type_is_community.yml": ValidationResults{
ValidationError{"maintenance.contractors", "contractors must not be present unless \"type\" is \"contract\"", 46, 3},
},
"maintenance_contractors_when_type_is_internal.yml": ValidationResults{
ValidationError{"maintenance.contractors", "contractors must not be present unless \"type\" is \"contract\"", 46, 3},
},
"maintenance_contractors_when_type_is_none.yml": ValidationResults{
ValidationError{"maintenance.contractors", "contractors must not be present unless \"type\" is \"contract\"", 46, 3},
},

// localisation
"localisation_availableLanguages_missing.yml": ValidationResults{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
publiccodeYmlVersion: "0.4"

name: Medusa
url: "https://github.com/italia/developers.italia.it.git"
softwareVersion: "dev"
releaseDate: "2017-04-15"

platforms:
- web

categories:
- cloud-management

developmentStatus: development

softwareType: "standalone/other"

description:
en:
localisedName: Medusa
shortDescription: >
A rather short description which
is probably useless
longDescription: >
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 158 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 316 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 474 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 632 characters.
features:
- Just one feature

legal:
license: AGPL-3.0-or-later

maintenance:
type: "community"

contractors:
# This should NOT be accepted NOR validated (the "until" is missing),
# because type is "community"
- name: "Contractor"
website: "https://example.org"

contacts:
- name: John Smith

localisation:
localisationReady: true
availableLanguages:
- en
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
publiccodeYmlVersion: "0.4"

name: Medusa
url: "https://github.com/italia/developers.italia.it.git"
softwareVersion: "dev"
releaseDate: "2017-04-15"

platforms:
- web

categories:
- cloud-management

developmentStatus: development

softwareType: "standalone/other"

description:
en:
localisedName: Medusa
shortDescription: >
A rather short description which
is probably useless
longDescription: >
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 158 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 316 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 474 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 632 characters.
features:
- Just one feature

legal:
license: AGPL-3.0-or-later

maintenance:
type: "internal"

contractors:
# This should NOT be accepted NOR validated (the "until" is missing),
# because type is "internal"
- name: "Contractor"
website: "https://example.org"

contacts:
- name: John Smith

localisation:
localisationReady: true
availableLanguages:
- en
55 changes: 55 additions & 0 deletions testdata/v0/invalid/maintenance_contractors_when_type_is_none.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
publiccodeYmlVersion: "0.4"

name: Medusa
url: "https://github.com/italia/developers.italia.it.git"
softwareVersion: "dev"
releaseDate: "2017-04-15"

platforms:
- web

categories:
- cloud-management

developmentStatus: development

softwareType: "standalone/other"

description:
en:
localisedName: Medusa
shortDescription: >
A rather short description which
is probably useless
longDescription: >
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 158 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 316 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 474 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 632 characters.
features:
- Just one feature

legal:
license: AGPL-3.0-or-later

maintenance:
type: "none"

contractors:
# This should NOT be accepted NOR validated (the "until" is missing),
# because type is "none"
- name: "Contractor"
website: "https://example.org"

localisation:
localisationReady: true
availableLanguages:
- en
56 changes: 56 additions & 0 deletions testdata/v0/valid/maintenance_contacts_when_type_is_contract.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
publiccodeYmlVersion: "0.4"

name: Medusa
url: "https://github.com/italia/developers.italia.it.git"

platforms:
- web

categories:
- cloud-management

developmentStatus: development

softwareType: "standalone/other"

description:
en_GB:
localisedName: Medusa
shortDescription: >
A rather short description which
is probably useless
longDescription: >
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 158 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 316 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 474 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 632 characters.
features:
- Just one feature

legal:
license: AGPL-3.0-or-later

maintenance:
type: "contract"

contractors:
- name: "Contractor"
website: "https://example.org"
until: 2040-01-01

# `contacts` is allowed when type is "contract" as well
contacts:
- name: John Smith

localisation:
localisationReady: true
availableLanguages:
- en
51 changes: 51 additions & 0 deletions testdata/v0/valid/maintenance_contacts_when_type_is_internal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
publiccodeYmlVersion: "0.4"

name: Medusa
url: "https://github.com/italia/developers.italia.it.git"

platforms:
- web

categories:
- cloud-management

developmentStatus: development

softwareType: "standalone/other"

description:
en_GB:
localisedName: Medusa
shortDescription: >
A rather short description which
is probably useless
longDescription: >
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 158 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 316 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 474 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 632 characters.
features:
- Just one feature

legal:
license: AGPL-3.0-or-later

maintenance:
type: "internal"

# `contacts` is allowed when type is "internal" as well
contacts:
- name: John Smith

localisation:
localisationReady: true
availableLanguages:
- en
51 changes: 51 additions & 0 deletions testdata/v0/valid/maintenance_contacts_when_type_is_none.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
publiccodeYmlVersion: "0.4"

name: Medusa
url: "https://github.com/italia/developers.italia.it.git"

platforms:
- web

categories:
- cloud-management

developmentStatus: development

softwareType: "standalone/other"

description:
en_GB:
localisedName: Medusa
shortDescription: >
A rather short description which
is probably useless
longDescription: >
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 158 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 316 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 474 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 632 characters.
features:
- Just one feature

legal:
license: AGPL-3.0-or-later

maintenance:
type: "none"

# `contacts` is allowed when type is "none" as well
contacts:
- name: John Smith

localisation:
localisationReady: true
availableLanguages:
- en
2 changes: 1 addition & 1 deletion v0.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type PublicCodeV0 struct {

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

Expand Down
20 changes: 20 additions & 0 deletions validators/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ func RegisterLocalErrorMessages(v *validator.Validate, trans ut.Translator) erro
},
override: true,
},
{
// Override the default error with a more user friendly one
//
// original:
// foo is an excluded field
// overridden:
// foo is not permitted when "bar" is "foobar"
tag: "excluded_unless",
customRegisFunc: func(ut ut.Translator) error {
return ut.Add("excluded_unless", "{0} must not be present unless {1}", true)
},
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
parts := strings.Fields(fe.Param())

t, _ := ut.T("excluded_unless", fe.Field(), fmt.Sprintf("\"%s\" is \"%s\"", strings.ToLower(parts[0]), parts[1]))

return t
},
override: true,
},
{
tag: "umax",
customRegisFunc: func(ut ut.Translator) error {
Expand Down