diff --git a/spaceapi_validator.go b/spaceapi_validator.go index 96149ae..695184b 100644 --- a/spaceapi_validator.go +++ b/spaceapi_validator.go @@ -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] @@ -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 != "" { + versionList = append(versionList, oldVersion) + } + + if len(versionList) == 0 { + versionList = []string{"14"} + } + return versionList +} diff --git a/spaceapi_validator_test.go b/spaceapi_validator_test.go index 7352ea2..0577bae 100644 --- a/spaceapi_validator_test.go +++ b/spaceapi_validator_test.go @@ -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) @@ -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")