Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 1bd28e6

Browse files
committed
Improve manifest validation
- Improves property validation by checking for TOML array of tables. - Adds checks for invalid fields in dependencies and overrides.
1 parent 8129217 commit 1bd28e6

File tree

2 files changed

+62
-27
lines changed

2 files changed

+62
-27
lines changed

manifest.go

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"fmt"
1010
"io"
11-
"reflect"
1211
"sort"
1312

1413
"github.com/golang/dep/gps"
@@ -19,10 +18,6 @@ import (
1918

2019
const ManifestName = "Gopkg.toml"
2120

22-
var ManifestFields = []string{
23-
"dependencies", "overrides", "ignored", "required", "metadata",
24-
}
25-
2621
type Manifest struct {
2722
Dependencies gps.ProjectConstraints
2823
Ovr gps.ProjectConstraints
@@ -57,31 +52,37 @@ func validateManifest(s string) ([]error, error) {
5752

5853
// Look for unknown fields and raise warnings
5954
for prop, val := range manifest {
60-
isValidField := false
61-
for _, v := range ManifestFields {
62-
if prop == v {
63-
isValidField = true
64-
65-
// For metadata, check if it is a map type
66-
if prop == "metadata" {
67-
isValidMetadata := false
68-
if metadata, ok := val.([]interface{}); ok {
69-
for _, v := range metadata {
70-
if reflect.TypeOf(v).Kind() == reflect.Map {
71-
isValidMetadata = true
72-
} else {
73-
isValidMetadata = false
74-
break
75-
}
55+
switch prop {
56+
case "metadata":
57+
// Invalid if type assertion fails. Not a TOML array of tables.
58+
// This check is enough for map validation because any non-key-value
59+
// pair inside array of tables becomes an invalid TOML and parser
60+
// would fail.
61+
if _, ok := val.([]interface{}); !ok {
62+
errs = append(errs, errors.New("metadata should be a TOML array of tables"))
63+
}
64+
case "dependencies", "overrides":
65+
// Invalid if type assertion fails. Not a TOML array of tables.
66+
if rawProj, ok := val.([]interface{}); ok {
67+
// Iterate through each array of tables
68+
for _, v := range rawProj {
69+
// Check the individual field's key to be valid
70+
for key, _ := range v.(map[string]interface{}) {
71+
// Check if the key is valid
72+
switch key {
73+
case "name", "branch", "revision", "version", "source":
74+
// valid key
75+
default:
76+
// unknown/invalid key
77+
errs = append(errs, fmt.Errorf("Invalid key %q in %q", key, prop))
7678
}
7779
}
78-
if !isValidMetadata {
79-
errs = append(errs, errors.New("metadata should consist of key-value pairs"))
80-
}
8180
}
81+
} else {
82+
errs = append(errs, fmt.Errorf("%v should be a TOML array of tables", prop))
8283
}
83-
}
84-
if !isValidField {
84+
case "ignored", "required":
85+
default:
8586
errs = append(errs, fmt.Errorf("Unknown field in manifest: %v", prop))
8687
}
8788
}

manifest_test.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ func TestValidateManifest(t *testing.T) {
133133
`,
134134
want: []error{},
135135
},
136+
{
137+
tomlString: `
138+
[[metadata]]
139+
authors = "foo"
140+
version = "1.0.0"
141+
`,
142+
want: []error{},
143+
},
136144
{
137145
tomlString: `
138146
foo = "some-value"
@@ -158,7 +166,33 @@ func TestValidateManifest(t *testing.T) {
158166
[[dependencies]]
159167
name = "github.com/foo/bar"
160168
`,
161-
want: []error{errors.New("metadata should consist of key-value pairs")},
169+
want: []error{errors.New("metadata should be a TOML array of tables")},
170+
},
171+
{
172+
tomlString: `
173+
dependencies = "foo"
174+
overrides = "bar"
175+
`,
176+
want: []error{
177+
errors.New("dependencies should be a TOML array of tables"),
178+
errors.New("overrides should be a TOML array of tables"),
179+
},
180+
},
181+
{
182+
tomlString: `
183+
[[dependencies]]
184+
name = "github.com/foo/bar"
185+
location = "some-value"
186+
link = "some-other-value"
187+
188+
[[overrides]]
189+
nick = "foo"
190+
`,
191+
want: []error{
192+
errors.New("Invalid key \"location\" in \"dependencies\""),
193+
errors.New("Invalid key \"link\" in \"dependencies\""),
194+
errors.New("Invalid key \"nick\" in \"overrides\""),
195+
},
162196
},
163197
}
164198

0 commit comments

Comments
 (0)