Skip to content

Commit f4ff188

Browse files
committed
feat: differentiate between "v14-style" versioning (.api) and "v12-style" (.api_compatibility) versioning
1 parent 26b8331 commit f4ff188

File tree

4 files changed

+100
-32
lines changed

4 files changed

+100
-32
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Possible log types:
1313

1414
### Unreleased
1515

16+
- [added] Support for schema 16-draft
17+
- [changed] Fail validation on usage of wrong versioning scheme (i.e. `.api` vs `.api_compatibility`)
18+
1619
### v0.2.0 (2024-12-29)
1720

1821
- [added] Support for schema v15

spaceapi_validator.go

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,30 @@ func Validate(document string) (ValidationResult, error) {
5353
return myResult, err
5454
}
5555

56-
versionList := getVersionList(suppliedVersion)
56+
versionList, invalidVersions := getVersionList(suppliedVersion)
57+
58+
for _, version := range invalidVersions {
59+
// Version not found. Thus, we cannot validate. Show an
60+
// error for the declared "api_compatibilty" and continue.
61+
myResult.Valid = false
62+
invalidVersionErrors := []ResultError{
63+
{
64+
"api_compatibility",
65+
"(root).api_compatibility",
66+
fmt.Sprintf("Endpoint declares compatibility with schema version %s, which isn't supported", version),
67+
},
68+
}
69+
myResult.Schemas = append(myResult.Schemas, VersionValidationResult{
70+
version,
71+
false,
72+
invalidVersionErrors,
73+
})
74+
myResult.Errors = append(myResult.Errors, invalidVersionErrors...)
75+
}
5776

5877
for _, version := range versionList {
59-
schemaString, found := SpaceAPISchemas[version]
60-
if !found {
61-
// Version not found. Thus, we cannot validate. Show an
62-
// error for the declared "api_compatibilty" and continue.
63-
myResult.Valid = false
64-
invalidVersionErrors := []ResultError{
65-
{
66-
"api_compatibility",
67-
"(root).api_compatibility",
68-
fmt.Sprintf("Endpoint declares compatibility with schema version %s, which isn't supported", version),
69-
},
70-
}
71-
myResult.Schemas = append(myResult.Schemas, VersionValidationResult{
72-
version,
73-
false,
74-
invalidVersionErrors,
75-
})
76-
myResult.Errors = append(myResult.Errors, invalidVersionErrors...)
77-
continue
78-
}
79-
var schema = gojsonschema.NewStringLoader(schemaString)
78+
schemaVersion := SpaceAPISchemas[version]
79+
var schema = gojsonschema.NewStringLoader(schemaVersion)
8080
result, err := gojsonschema.Validate(schema, documentLoader)
8181
if err != nil {
8282
myResult.Valid = false
@@ -108,15 +108,27 @@ func Validate(document string) (ValidationResult, error) {
108108
return myResult, err
109109
}
110110

111-
func getVersionList(suppliedVersion spaceAPIVersion) []string {
112-
versionList := suppliedVersion.APICompatibility
113-
oldVersion := strings.Replace(fmt.Sprintf("%v", suppliedVersion.API), "0.", "", 1)
114-
if oldVersion != "<nil>" {
115-
versionList = append(versionList, oldVersion)
111+
func getVersionList(suppliedVersion spaceAPIVersion) ([]string, []string) {
112+
var versionList = []string{}
113+
var invalidVersions = []string{}
114+
for _, v14version := range suppliedVersion.APICompatibility {
115+
if schema, found := SpaceApiVersioning[v14version]; found && schema == V14 {
116+
versionList = append(versionList, v14version)
117+
} else {
118+
invalidVersions = append(invalidVersions, v14version)
119+
}
120+
}
121+
v12version := strings.Replace(fmt.Sprintf("%v", suppliedVersion.API), "0.", "", 1)
122+
if v12version != "<nil>" {
123+
if schema, found := SpaceApiVersioning[v12version]; found && schema == V12 {
124+
versionList = append(versionList, v12version)
125+
} else {
126+
invalidVersions = append(invalidVersions, v12version)
127+
}
116128
}
117129

118-
if len(versionList) == 0 {
119-
versionList = []string{"14"}
130+
if len(versionList) == 0 && len(invalidVersions) == 0 {
131+
versionList = []string{"15"}
120132
}
121-
return versionList
133+
return versionList, invalidVersions
122134
}

spaceapi_validator_test.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ var missingIssueReportChannel13 = `{
7979
}
8080
}`
8181
var noVersion = `{ "data": "asd" }`
82+
var wrongVersionUsage = `{ "api": "14", "api_compatibility": [ "13", "0.13" ], "space": "example", "url": "https://example.com", "logo": "https://example.com/logo.png", "location": { "lon": 42, "lat": 23 }, "state": { "open": true }, "contact": {} }`
8283

8384
func TestValidate(t *testing.T) {
8485
invalidResult, _ := Validate(invalid13)
@@ -150,9 +151,9 @@ func TestValidate(t *testing.T) {
150151
t.Error("Expected validation to be false, got", invalidResult.Valid)
151152
}
152153
invalidErrors = invalidResult.Errors
153-
if len(invalidErrors) != 6 {
154+
if len(invalidErrors) != 5 {
154155
t.Logf("%v", invalidResult)
155-
t.Error("Schema should have got 6 errors, got", len(invalidErrors))
156+
t.Error("Schema should have got 5 errors, got", len(invalidErrors))
156157
}
157158

158159
validResult, err := Validate("")
@@ -187,4 +188,38 @@ func TestValidate(t *testing.T) {
187188
if !strings.Contains(invalidErrors[0].Description, "Endpoint declares compatibility with schema version 142, which isn't supported") {
188189
t.Error("Did not find expected 'unknown schema version' error:", invalidErrors[0].Description)
189190
}
191+
192+
invalidResult, err = Validate(wrongVersionUsage)
193+
if err != nil {
194+
t.Error("validation error shouldn't show up on valid json")
195+
} else if invalidResult.Valid == true {
196+
t.Error("Expected validation to be false, got true")
197+
}
198+
invalidErrors = invalidResult.Errors
199+
if len(invalidErrors) != 3 {
200+
t.Logf("%v", invalidResult)
201+
t.Error("Schema should have got 3 errors, got", len(invalidErrors))
202+
}
203+
if !strings.Contains(invalidErrors[0].Description, "Endpoint declares compatibility with schema version 13, which isn't supported") {
204+
t.Error("Did not find expected 'unknown schema version' error:", invalidErrors[0].Description)
205+
}
206+
if !strings.Contains(invalidErrors[1].Description, "Endpoint declares compatibility with schema version 0.13, which isn't supported") {
207+
t.Error("Did not find expected 'unknown schema version' error:", invalidErrors[1].Description)
208+
}
209+
if !strings.Contains(invalidErrors[2].Description, "Endpoint declares compatibility with schema version 14, which isn't supported") {
210+
t.Error("Did not find expected 'unknown schema version' error:", invalidErrors[2].Description)
211+
}
212+
}
213+
214+
func TestSchemaVersioningMatch(t *testing.T) {
215+
for v, _ := range SpaceAPISchemas {
216+
if _, found := SpaceApiVersioning[v]; !found {
217+
t.Error("Version", v, "from schemas.SpaceAPISchemas missing in versioning.SpaceApiVersioning")
218+
}
219+
}
220+
for v, _ := range SpaceApiVersioning {
221+
if _, found := SpaceAPISchemas[v]; !found {
222+
t.Error("Version", v, "from versioning.SpaceApiVersioning missing in schemas.SpaceAPISchemas")
223+
}
224+
}
190225
}

versioning.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package spaceapivalidator
2+
3+
type VersionSchema int
4+
5+
const (
6+
// versioning scheme introduced in v0.12: ".api" key
7+
V12 VersionSchema = iota
8+
// versioning scheme introduced in v14: ".api_compatibility" list
9+
V14
10+
)
11+
12+
var SpaceApiVersioning = map[string]VersionSchema {
13+
"12": V12,
14+
"13": V12,
15+
"14": V14,
16+
"15": V14,
17+
"16": V14,
18+
}

0 commit comments

Comments
 (0)