Skip to content

Commit

Permalink
Fix validation of version less JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
gidsi committed Nov 14, 2020
1 parent f891003 commit 329615a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
23 changes: 14 additions & 9 deletions spaceapi_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,7 @@ func Validate(document string) (ValidationResult, error) {
return myResult, err
}

versionList := suppliedVersion.APICompatibility
oldVersion := strings.Replace(fmt.Sprintf("%v", suppliedVersion.API), "0.", "", 1)
if oldVersion != "" {
versionList = append(versionList, oldVersion)
}

if len(versionList) == 0 {
versionList = []string{"14"}
}
versionList := getVersionList(suppliedVersion)

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

return myResult, err
}

func getVersionList(suppliedVersion spaceAPIVersion) []string {
versionList := suppliedVersion.APICompatibility
oldVersion := strings.Replace(fmt.Sprintf("%v", suppliedVersion.API), "0.", "", 1)
if oldVersion != "<nil>" {
versionList = append(versionList, oldVersion)
}

if len(versionList) == 0 {
versionList = []string{"14"}
}
return versionList
}
11 changes: 11 additions & 0 deletions spaceapi_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var invalid14 = `{ "api_compatibility": [ "14" ], "space_invalid": "example", "u
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": {} }`
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": {} }`
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" ] }`
var noVersion = `{ "data": "asd" }`

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

invalidResult, _ = Validate(noVersion)
if invalidResult.Valid == true {
t.Error("Expected validation to be false, got", invalidResult.Valid)
}
invalidErrors = invalidResult.Errors
if len(invalidErrors) != 6 {
t.Logf("%v", invalidResult)
t.Error("Schema should have got 1 errors, got", len(invalidErrors))
}

validResult, err := Validate("")
if err == nil {
t.Error("Should provide an error on faulty json")
Expand Down

0 comments on commit 329615a

Please sign in to comment.