Skip to content

Commit 1e32ccd

Browse files
committed
Fix validation with multiple versions with some invalid
1 parent 329615a commit 1e32ccd

2 files changed

Lines changed: 77 additions & 3 deletions

File tree

spaceapi_validator.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type ValidationResult struct {
4141

4242
// Validate a string to match jsonschema of SpaceApi
4343
func Validate(document string) (ValidationResult, error) {
44-
myResult := ValidationResult{}
44+
myResult := ValidationResult{ Valid: true }
4545
if document == "" {
4646
return myResult, errors.New("document is empty")
4747
}
@@ -60,6 +60,7 @@ func Validate(document string) (ValidationResult, error) {
6060
var schema = gojsonschema.NewStringLoader(schemaString)
6161
result, err := gojsonschema.Validate(schema, documentLoader)
6262
if err != nil {
63+
myResult.Valid = false
6364
return myResult, err
6465
}
6566

@@ -80,8 +81,8 @@ func Validate(document string) (ValidationResult, error) {
8081

8182
myResult.Errors = append(myResult.Errors, myErrors...)
8283

83-
if result.Valid() {
84-
myResult.Valid = true
84+
if !result.Valid() {
85+
myResult.Valid = false
8586
}
8687
}
8788

spaceapi_validator_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,70 @@ var invalid14 = `{ "api_compatibility": [ "14" ], "space_invalid": "example", "u
1212
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": {} }`
1313
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": {} }`
1414
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" ] }`
15+
var missingIssueReportChannel13 = `{
16+
"api": "0.13",
17+
"api_compatibility": ["14"],
18+
"space": "NYCResistor",
19+
"logo": "https://www.nycresistor.com/wp-content/uploads/2017/09/NYCR-logo-wide.png",
20+
"url": "https://www.nycresistor.com/",
21+
"location": {
22+
"address": "87 3rd Avenue, 4th floor, Brooklyn, NY 11217, USA",
23+
"lon": 40.683653,
24+
"lat": -73.981542,
25+
"timezone": "America/New_York"
26+
},
27+
"contact": {
28+
"email": "contact@nycresistor.com",
29+
"matrix": "#public:nycr.chat",
30+
"facebook": "https://www.facebook.com/groups/23644223800/",
31+
"ml": "nycresistormicrocontrollers@googlegroups.com"
32+
},
33+
"state": {
34+
"open": false
35+
},
36+
"links": [
37+
{
38+
"name": "eventbrite",
39+
"url": "http://nycresistor.eventbrite.com/"
40+
},
41+
{
42+
"name": "code of conduct",
43+
"url": "https://www.nycresistor.com/participate/"
44+
},
45+
{
46+
"name": "withfriends",
47+
"url": "https://withfriends.co/nyc_resistor/join"
48+
}
49+
],
50+
"membership_plans": [
51+
{
52+
"name": "standard",
53+
"value": 115,
54+
"currency": "USD",
55+
"billing_interval": "monthly"
56+
},
57+
{
58+
"name": "teaching",
59+
"value": 75,
60+
"currency": "USD",
61+
"billing_interval": "monthly"
62+
}
63+
],
64+
"feeds": {
65+
"blog": {
66+
"type": "rss",
67+
"url": "https://www.nycresistor.com/feed/"
68+
},
69+
"wiki": {
70+
"type": "atom",
71+
"url": "https://wiki.nycresistor.com/w/api.php?hidebots=1&urlversion=1&days=30&limit=50&action=feedrecentchanges&feedformat=atom"
72+
},
73+
"calendar": {
74+
"type": "ical",
75+
"url": "https://calendar.google.com/calendar/ical/p2m2av9dhrh4n1ub7jlsc68s7o%40group.calendar.google.com/public/basic.ics"
76+
}
77+
}
78+
}`
1579
var noVersion = `{ "data": "asd" }`
1680

1781
func TestValidate(t *testing.T) {
@@ -70,6 +134,15 @@ func TestValidate(t *testing.T) {
70134
t.Error("Schema should have got 1 errors, got", len(invalidErrors))
71135
}
72136

137+
invalidResult, _ = Validate(missingIssueReportChannel13)
138+
if invalidResult.Valid == true {
139+
t.Error("Expected validation to be false, got", invalidResult.Valid)
140+
}
141+
invalidErrors = invalidResult.Errors
142+
if len(invalidErrors) != 1 {
143+
t.Error("Schema should have got 1 errors, got", len(invalidErrors))
144+
}
145+
73146
invalidResult, _ = Validate(noVersion)
74147
if invalidResult.Valid == true {
75148
t.Error("Expected validation to be false, got", invalidResult.Valid)

0 commit comments

Comments
 (0)