diff --git a/spaceapi_validator.go b/spaceapi_validator.go index 237b05f..6955575 100644 --- a/spaceapi_validator.go +++ b/spaceapi_validator.go @@ -5,6 +5,7 @@ package spaceapivalidator import ( "encoding/json" "errors" + "fmt" "github.com/xeipuuv/gojsonschema" "strings" ) @@ -12,7 +13,7 @@ import ( //go:generate go run scripts/generate.go type spaceAPIVersion struct { - API string + API interface{} } // ResultError tells you whats wrong with specific attributes of your SpaceApi file @@ -43,7 +44,7 @@ func Validate(document string) (ValidationResult, error) { return myResult, err } - schemaString, ok := SpaceAPISchemas[strings.Replace(suppliedVersion.API, "0.", "", 1)] + schemaString, ok := SpaceAPISchemas[strings.Replace(fmt.Sprintf("%v", suppliedVersion.API), "0.", "", 1)] if !ok { schemaString = SpaceAPISchemas["13"] } diff --git a/spaceapi_validator_test.go b/spaceapi_validator_test.go index b644dcc..e3e0077 100644 --- a/spaceapi_validator_test.go +++ b/spaceapi_validator_test.go @@ -7,6 +7,7 @@ import ( var wrongVersion = `{ "api": "0.5" }` var invalid13 = `{ "api": "0.13" }` var valid13 = `{ "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 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" ] }` func TestValidate(t *testing.T) { invalidResult, _ := Validate(invalid13) @@ -37,4 +38,11 @@ func TestValidate(t *testing.T) { if invalidResult.Valid == true { t.Error("Expected validation to be false, got", invalidResult.Valid) } + + invalidResult, err = Validate(wrongVersionNumeric) + if err != nil { + t.Error("validation error shouldn't show up on valid json") + } else if invalidResult.Valid == true { + t.Error("Expected validation to be false, got", invalidResult.Valid) + } }