Skip to content

Commit a23275d

Browse files
authored
Invalid JSON should not be considered an error - see #67 (#327)
1 parent 3134f44 commit a23275d

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

pkg/loader/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (l *HTTPURLLoader) Load(url string) (any, error) {
5656

5757
s, err := jsonschema.UnmarshalJSON(bytes.NewReader(body))
5858
if err != nil {
59-
return nil, err
59+
return nil, NewNonJSONResponseError(err)
6060
}
6161

6262
return s, nil

pkg/loader/loaders.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@ func NewNotFoundError(err error) *NotFoundError {
1010
}
1111
func (e *NotFoundError) Error() string { return e.err.Error() }
1212
func (e *NotFoundError) Retryable() bool { return false }
13+
14+
type NonJSONResponseError struct {
15+
err error
16+
}
17+
18+
func NewNonJSONResponseError(err error) *NotFoundError {
19+
return &NotFoundError{err}
20+
}
21+
func (e *NonJSONResponseError) Error() string { return e.err.Error() }
22+
func (e *NonJSONResponseError) Retryable() bool { return false }

pkg/validator/validator.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ func downloadSchema(registries []registry.Registry, l jsonschema.SchemeURLLoader
309309
if _, notfound := err.(*loader.NotFoundError); notfound {
310310
continue
311311
}
312+
if _, nonJSONError := err.(*loader.NonJSONResponseError); nonJSONError {
313+
continue
314+
}
315+
312316
return nil, err
313317
}
314318

pkg/validator/validator_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ lastName: bar
316316
}`),
317317
false,
318318
false,
319-
Error,
319+
Valid,
320320
[]ValidationError{},
321321
},
322322
{
@@ -361,7 +361,7 @@ lastName: bar
361361
[]byte(`<html>error page</html>`),
362362
true,
363363
false,
364-
Error,
364+
Skipped,
365365
[]ValidationError{},
366366
},
367367
{
@@ -394,13 +394,19 @@ lastName: bar
394394
return "", nil, loader.NewNotFoundError(nil)
395395
}
396396
s, err := jsonschema.UnmarshalJSON(bytes.NewReader(testCase.schemaRegistry1))
397+
if err != nil {
398+
return "", s, loader.NewNonJSONResponseError(err)
399+
}
397400
return "", s, err
398401
}),
399402
newMockRegistry(func() (string, any, error) {
400403
if testCase.schemaRegistry2 == nil {
401404
return "", nil, loader.NewNotFoundError(nil)
402405
}
403406
s, err := jsonschema.UnmarshalJSON(bytes.NewReader(testCase.schemaRegistry2))
407+
if err != nil {
408+
return "", s, loader.NewNonJSONResponseError(err)
409+
}
404410
return "", s, err
405411
}),
406412
},
@@ -469,6 +475,9 @@ age: not a number
469475
regs: []registry.Registry{
470476
newMockRegistry(func() (string, any, error) {
471477
s, err := jsonschema.UnmarshalJSON(bytes.NewReader(schema))
478+
if err != nil {
479+
return "", s, loader.NewNonJSONResponseError(err)
480+
}
472481
return "", s, err
473482
}),
474483
},

0 commit comments

Comments
 (0)