Skip to content

feat: contractors and contacts validation #208

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
24 changes: 24 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "PublicCodeGoParser",
"runArgs": [
"--name=PublicCodeGoParser"
],
"image": "mcr.microsoft.com/devcontainers/go:1-1.23-bookworm",
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {
"moby": true,
"azureDnsAutoDetection": true,
"installDockerBuildx": true,
"installDockerComposeSwitch": true,
"version": "latest",
"dockerDashComposeVersion": "v2"
}
},
"customizations": {
"vscode": {
"extensions": [
"redhat.vscode-yaml"
]
}
}
}
3 changes: 3 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) {
"iso3166_1_alpha2_lowercase": "must be a valid lowercase ISO 3166-1 alpha-2 two-letter country code",
"bcp47_language_tag": "must be a valid BCP 47 language",
"bcp47_keys": "must use a valid BCP 47 language",
"required_if": "must be present if",
"excluded_unless": "must not be present unless",
"excluded_unless=Type community|excluded_unless=Type internal": "must not be present unless Type is community or internal",
}
for _, err := range err.(validator.ValidationErrors) {
var sb strings.Builder
Expand Down
44 changes: 39 additions & 5 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,16 +458,31 @@ func TestInvalidTestcasesV0(t *testing.T) {
// maintenance
// maintenance.*
"maintenance_type_missing.yml": ValidationResults{
ValidationError{"maintenance.type", "required", 47, 3},
ValidationError{"maintenance.type", "required", 43, 13},
},
"maintenance_type_invalid.yml": ValidationResults{
ValidationError{"maintenance.type", "must be one of the following: internal contract community none", 45, 3},
},
"maintenance_contractors_excluded_if_type_community.yml": ValidationResults{
ValidationError{"maintenance.contractors", "must not be present unless Type contract", 47, 3},
},
"maintenance_contractors_excluded_if_type_internal.yml": ValidationResults{
ValidationError{"maintenance.contractors", "must not be present unless Type contract", 47, 3},
},
"maintenance_contractors_excluded_if_type_none.yml": ValidationResults{
ValidationError{"maintenance.contractors", "must not be present unless Type contract", 47, 3},
},
"maintenance_contacts_missing_with_type_community.yml": ValidationResults{
ValidationError{"maintenance.contacts", "required_if Type community", 44, 3},
ValidationError{"maintenance.contacts", "must be present if Type community", 44, 3},
},
"maintenance_contacts_missing_with_type_internal.yml": ValidationResults{
ValidationError{"maintenance.contacts", "required_if Type internal", 44, 3},
ValidationError{"maintenance.contacts", "must be present if Type internal", 44, 3},
},
"maintenance_contacts_excluded_if_type_contract.yml": ValidationResults{
ValidationError{"maintenance.contacts", "must not be present unless Type is community or internal Type internal", 51, 3},
},
"maintenance_contacts_excluded_if_type_none.yml": ValidationResults{
ValidationError{"maintenance.contacts", "must not be present unless Type is community or internal Type internal", 46, 3},
},
"maintenance_contacts_name_missing.yml": ValidationResults{
ValidationError{"maintenance.contacts[0].name", "required", 0, 0},
Expand All @@ -476,11 +491,11 @@ func TestInvalidTestcasesV0(t *testing.T) {
ValidationError{"maintenance.contacts[0].email", "must be a valid email", 0, 0},
},
"maintenance_contractors_missing_with_type_contract.yml": ValidationResults{
ValidationError{"maintenance.contractors", "required_if Type contract", 44, 3},
ValidationError{"maintenance.contractors", "must be present if Type contract", 44, 3},
},
"maintenance_contractors_invalid_type.yml": ValidationResults{
ValidationError{"maintenance.contractors", "wrong type for this field", 47, 1},
ValidationError{"maintenance.contractors", "required_if Type contract", 47, 3},
ValidationError{"maintenance.contractors", "must be present if Type contract", 47, 3},
},
"maintenance_contractors_name_missing.yml": ValidationResults{
ValidationError{"maintenance.contractors[0].name", "required", 0, 0},
Expand Down Expand Up @@ -736,3 +751,22 @@ func TestToURL(t *testing.T) {
}
}
}

func TestInvalidContractorsMustBeIgnored(t *testing.T) {
expected := error(
ValidationResults{
ValidationError{"maintenance.contractors", "must not be present unless Type contract", 47, 3},
},
)

parser, errParser := NewParser(ParserConfig{DisableNetwork: true})
if errParser != nil {
t.Errorf("Can't create Parser: %v", errParser)
}
var file = "testdata/v0/invalid/maintenance_contractors_excluded_if_type_community.yml"
_, err := parser.Parse(file)

if err != nil && expected != nil && err.Error() != expected.Error() {
t.Errorf("Expected contractors excluded_if error")
}
}
14 changes: 0 additions & 14 deletions testdata/v0/invalid/landingURL_invalid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,6 @@ maintenance:
website: "https://developers.italia.it"
until: "2019-01-01"

contacts:
- name: Francesco Rossi
email: "[email protected]"
affiliation: Comune di Reggio Emilia
phone: +39 231 13215112
- name: Dario Bianchi
email: "[email protected]"
affiliation: Fornitore Privato S.P.A.
phone: +39 16 24231322
- name: Giancarlo Verdi
email: "[email protected]"
affiliation: Fornitore Privato S.P.A.
phone: +39 16 24231322

localisation:
localisationReady: true
availableLanguages:
Expand Down
14 changes: 0 additions & 14 deletions testdata/v0/invalid/landingURL_wrong_type.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,6 @@ maintenance:
website: "https://developers.italia.it"
until: "2019-01-01"

contacts:
- name: Francesco Rossi
email: "[email protected]"
affiliation: Comune di Reggio Emilia
phone: +39 231 13215112
- name: Dario Bianchi
email: "[email protected]"
affiliation: Fornitore Privato S.P.A.
phone: +39 16 24231322
- name: Giancarlo Verdi
email: "[email protected]"
affiliation: Fornitore Privato S.P.A.
phone: +39 16 24231322

localisation:
localisationReady: true
availableLanguages:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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: "contract"

# Should NOT validate: invalid payload
contractors:
- name: "Fornitore Privato SPA"
website: "https://developers.italia.it"
until: "2019-01-01"
contacts:
- name: Francesco Rossi

localisation:
localisationReady: true
availableLanguages:
- en
52 changes: 52 additions & 0 deletions testdata/v0/invalid/maintenance_contacts_excluded_if_type_none.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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"

contacts:
- name: Francesco Rossi

localisation:
localisationReady: true
availableLanguages:
- en
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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"

# Should NOT validate: invalid payload
contractors:
- name: "Fornitore Privato SPA"
website: "https://developers.italia.it"
# until: "2019-01-01"
contacts:
- name: Francesco Rossi

localisation:
localisationReady: true
availableLanguages:
- en
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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"

# Should NOT validate: invalid payload
contractors:
- name: "Fornitore Privato SPA"
website: "https://developers.italia.it"
# until: "2019-01-01"
contacts:
- name: Francesco Rossi

localisation:
localisationReady: true
availableLanguages:
- en
Loading