Skip to content
Closed
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
83 changes: 51 additions & 32 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"slices"
"strings"
Expand Down Expand Up @@ -804,42 +805,60 @@ func decode[T any](data []byte, publiccode *T, file *ast.File) (ve ValidationRes

d := yaml.NewDecoder(bytes.NewReader(data), yaml.DisallowUnknownField())

if err := d.Decode(publiccode); err != nil {
var (
unknownErr *yaml.UnknownFieldError
yamlErr yaml.Error
)
err := d.Decode(publiccode)
if err == nil {
return ve
}

switch {
case errors.As(err, &unknownErr):
line := 0
if unknownErr.Token != nil {
line = unknownErr.Token.Position.Line
}
// The decoder reports one unknown key at most and stops at the first
// error, so look for all of them whatever error it returned.
for _, key := range findUnknownKeys(file.Docs[0].Body, reflect.TypeFor[T]()) {
ve = append(ve, ValidationError{
Key: key.path,
Description: fmt.Sprintf("unknown field %q", key.name),
Line: key.token.Position.Line,
Column: 1,
})
}

key := findKeyAtLine(file.Docs[0].Body, line, "")
ve = append(ve, ValidationError{
Key: key,
Description: unknownErr.Message,
Line: line,
Column: 1,
})
case errors.As(err, &yamlErr):
line := 0
if tok := yamlErr.GetToken(); tok != nil {
line = tok.Position.Line
}
var (
unknownErr *yaml.UnknownFieldError
yamlErr yaml.Error
)

key := findKeyAtLine(file.Docs[0].Body, line, "")
ve = append(ve, ValidationError{
Key: key,
Description: "wrong type for this field",
Line: line,
Column: 1,
})
default:
ve = append(ve, newValidationError("", err.Error()))
switch {
case errors.As(err, &unknownErr):
if len(ve) > 0 {
break
}

line := 0
if unknownErr.Token != nil {
line = unknownErr.Token.Position.Line
}

key := findKeyAtLine(file.Docs[0].Body, line, "")
ve = append(ve, ValidationError{
Key: key,
Description: unknownErr.Message,
Line: line,
Column: 1,
})
case errors.As(err, &yamlErr):
line := 0
if tok := yamlErr.GetToken(); tok != nil {
line = tok.Position.Line
}

key := findKeyAtLine(file.Docs[0].Body, line, "")
ve = append(ve, ValidationError{
Key: key,
Description: "wrong type for this field",
Line: line,
Column: 1,
})
default:
ve = append(ve, newValidationError("", err.Error()))
}

return ve
Expand Down
97 changes: 97 additions & 0 deletions parser_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,103 @@ name:
}
}

func TestDecodeReportsEveryUnknownKey(t *testing.T) {
tests := map[string]struct {
yaml string
want ValidationResults
}{
"keys in a flow mapping": {
yaml: `publiccodeYmlVersion: "0"
legal: {license: MIT, b: 1, a: 2}
`,
want: ValidationResults{
ValidationError{"", "legal.b", `unknown field "b"`, 2, 1},
ValidationError{"", "legal.a", `unknown field "a"`, 2, 1},
},
},
"key of an anchor used twice": {
yaml: `publiccodeYmlVersion: "0"
fundedBy:
- &org {uri: "urn:x-test:a", foo: 1}
- *org
`,
want: ValidationResults{
ValidationError{"", "fundedBy[0].foo", `unknown field "foo"`, 3, 1},
},
},
"key brought in by a merge key": {
yaml: `publiccodeYmlVersion: "0"
x: &base {type: internal, bar: 1}
maintenance:
<<: *base
baz: 2
`,
want: ValidationResults{
ValidationError{"", "x", `unknown field "x"`, 2, 1},
ValidationError{"", "maintenance.bar", `unknown field "bar"`, 2, 1},
ValidationError{"", "maintenance.baz", `unknown field "baz"`, 5, 1},
},
},
"anchor holding an alias of itself": {
yaml: `publiccodeYmlVersion: "0"
fundedBy: &x [*x]
foo: 1
`,
want: ValidationResults{
ValidationError{"", "foo", `unknown field "foo"`, 3, 1},
ValidationError{"", "fundedBy", "wrong type for this field", 2, 1},
},
},
"mapping merging an alias of itself": {
yaml: `publiccodeYmlVersion: "0"
maintenance: &x {type: internal, foo: 1, <<: *x}
`,
want: ValidationResults{
ValidationError{"", "maintenance.foo", `unknown field "foo"`, 2, 1},
newValidationError("", "cannot find anchor by alias name x"),
},
},
"tagged, quoted and anchored keys": {
yaml: `publiccodeYmlVersion: "0"
!!str zzTagged: 1
"zzQuoted": 2
&k zzAnchored: 3
!!str 4: 4
`,
want: ValidationResults{
ValidationError{"", "zzTagged", `unknown field "zzTagged"`, 2, 1},
ValidationError{"", "zzQuoted", `unknown field "zzQuoted"`, 3, 1},
ValidationError{"", "zzAnchored", `unknown field "zzAnchored"`, 4, 1},
ValidationError{"", "4", `unknown field "4"`, 5, 1},
},
},
"key next to a wrong type": {
yaml: `publiccodeYmlVersion: "0"
name: [a]
foo: 1
`,
want: ValidationResults{
ValidationError{"", "foo", `unknown field "foo"`, 3, 1},
ValidationError{"", "name", "wrong type for this field", 2, 1},
},
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
file, err := parser.ParseBytes([]byte(test.yaml), 0)
if err != nil {
t.Fatalf("unexpected parse error: %v", err)
}

got := decode([]byte(test.yaml), &PublicCodeV0{}, file)
if got.Error() != test.want.Error() {
t.Errorf("got:\n%v\nwant:\n%v", got, test.want)
}
})
}
}

func TestToURLLocalPath(t *testing.T) {
// toURL with a local file path should return a file:// URL.
u, err := toURL("./testdata/v0/valid/valid.yml")
Expand Down
58 changes: 58 additions & 0 deletions testdata/v0/invalid/unknown_fields.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
publiccodeYmlVersion: "0"

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

# Should NOT validate: none of the keys named zzz* exist in publiccode.yml,
# and each must get its own error.
zzzTop: 1

platforms:
- web

categories:
- cloud-management

developmentStatus: development

softwareType: "standalone/other"

description:
en-GB:
localisedName: Medusa
zzzDescription: 2
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"

contacts:
- name: Francesco Rossi
zzzContact: 3

localisation:
localisationReady: true
availableLanguages:
- en

zzzLast: 4
Loading