Skip to content

Commit 464b560

Browse files
committed
Fix bug with numeric api field
1 parent aa622e4 commit 464b560

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

spaceapi_validator.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ package spaceapivalidator
55
import (
66
"encoding/json"
77
"errors"
8+
"fmt"
89
"github.com/xeipuuv/gojsonschema"
910
"strings"
1011
)
1112

1213
//go:generate go run scripts/generate.go
1314

1415
type spaceAPIVersion struct {
15-
API string
16+
API interface{}
1617
}
1718

1819
// ResultError tells you whats wrong with specific attributes of your SpaceApi file
@@ -43,7 +44,7 @@ func Validate(document string) (ValidationResult, error) {
4344
return myResult, err
4445
}
4546

46-
schemaString, ok := SpaceAPISchemas[strings.Replace(suppliedVersion.API, "0.", "", 1)]
47+
schemaString, ok := SpaceAPISchemas[strings.Replace(fmt.Sprintf("%v", suppliedVersion.API), "0.", "", 1)]
4748
if !ok {
4849
schemaString = SpaceAPISchemas["13"]
4950
}

spaceapi_validator_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
var wrongVersion = `{ "api": "0.5" }`
88
var invalid13 = `{ "api": "0.13" }`
99
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" ] }`
10+
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" ] }`
1011

1112
func TestValidate(t *testing.T) {
1213
invalidResult, _ := Validate(invalid13)
@@ -37,4 +38,11 @@ func TestValidate(t *testing.T) {
3738
if invalidResult.Valid == true {
3839
t.Error("Expected validation to be false, got", invalidResult.Valid)
3940
}
41+
42+
invalidResult, err = Validate(wrongVersionNumeric)
43+
if err != nil {
44+
t.Error("validation error shouldn't show up on valid json")
45+
} else if invalidResult.Valid == true {
46+
t.Error("Expected validation to be false, got", invalidResult.Valid)
47+
}
4048
}

0 commit comments

Comments
 (0)