Skip to content

Commit

Permalink
Fix bug with numeric api field
Browse files Browse the repository at this point in the history
  • Loading branch information
gidsi committed Dec 19, 2019
1 parent aa622e4 commit 464b560
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions spaceapi_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ package spaceapivalidator
import (
"encoding/json"
"errors"
"fmt"
"github.com/xeipuuv/gojsonschema"
"strings"
)

//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
Expand Down Expand Up @@ -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"]
}
Expand Down
8 changes: 8 additions & 0 deletions spaceapi_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 464b560

Please sign in to comment.