Skip to content

Commit 329615a

Browse files
committed
Fix validation of version less JSON
1 parent f891003 commit 329615a

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

spaceapi_validator.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,7 @@ func Validate(document string) (ValidationResult, error) {
5353
return myResult, err
5454
}
5555

56-
versionList := suppliedVersion.APICompatibility
57-
oldVersion := strings.Replace(fmt.Sprintf("%v", suppliedVersion.API), "0.", "", 1)
58-
if oldVersion != "" {
59-
versionList = append(versionList, oldVersion)
60-
}
61-
62-
if len(versionList) == 0 {
63-
versionList = []string{"14"}
64-
}
56+
versionList := getVersionList(suppliedVersion)
6557

6658
for _, version := range versionList {
6759
schemaString, _ := SpaceAPISchemas[version]
@@ -95,3 +87,16 @@ func Validate(document string) (ValidationResult, error) {
9587

9688
return myResult, err
9789
}
90+
91+
func getVersionList(suppliedVersion spaceAPIVersion) []string {
92+
versionList := suppliedVersion.APICompatibility
93+
oldVersion := strings.Replace(fmt.Sprintf("%v", suppliedVersion.API), "0.", "", 1)
94+
if oldVersion != "<nil>" {
95+
versionList = append(versionList, oldVersion)
96+
}
97+
98+
if len(versionList) == 0 {
99+
versionList = []string{"14"}
100+
}
101+
return versionList
102+
}

spaceapi_validator_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var invalid14 = `{ "api_compatibility": [ "14" ], "space_invalid": "example", "u
1212
var valid15 = `{ "api_compatibility": [ "15" ], "space": "example", "url": "https://example.com", "logo": "https://example.com/logo.png", "location": { "lon": 42, "lat": 23 }, "state": { "open": true }, "contact": {} }`
1313
var invalid15 = `{ "api_compatibility": [ "15" ], "space_invalid": "example", "url": "https://example.com", "logo": "https://example.com/logo.png", "location": { "lon": 42, "lat": 23 }, "state": { "open": true }, "contact": {} }`
1414
var wrongVersionNumeric = `{ "api": 0.13, "open": true, "space": "example", "url": "https://example.com", "logo": "https://example.com/logo.png", "location": { "lon": 42, "lat": 23 }, "state": { "open": true }, "contact": {}, "issue_report_channels": [ "email" ] }`
15+
var noVersion = `{ "data": "asd" }`
1516

1617
func TestValidate(t *testing.T) {
1718
invalidResult, _ := Validate(invalid13)
@@ -69,6 +70,16 @@ func TestValidate(t *testing.T) {
6970
t.Error("Schema should have got 1 errors, got", len(invalidErrors))
7071
}
7172

73+
invalidResult, _ = Validate(noVersion)
74+
if invalidResult.Valid == true {
75+
t.Error("Expected validation to be false, got", invalidResult.Valid)
76+
}
77+
invalidErrors = invalidResult.Errors
78+
if len(invalidErrors) != 6 {
79+
t.Logf("%v", invalidResult)
80+
t.Error("Schema should have got 1 errors, got", len(invalidErrors))
81+
}
82+
7283
validResult, err := Validate("")
7384
if err == nil {
7485
t.Error("Should provide an error on faulty json")

0 commit comments

Comments
 (0)