Invalid JSON should not be considered an error - see https://github.com/yannh/kubeconform/issues/67 (#327)

This commit is contained in:
Yann Hamon 2025-05-12 10:21:02 +02:00 committed by GitHub
parent 3134f4477e
commit a23275d5ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 26 additions and 3 deletions

View file

@ -56,7 +56,7 @@ func (l *HTTPURLLoader) Load(url string) (any, error) {
s, err := jsonschema.UnmarshalJSON(bytes.NewReader(body)) s, err := jsonschema.UnmarshalJSON(bytes.NewReader(body))
if err != nil { if err != nil {
return nil, err return nil, NewNonJSONResponseError(err)
} }
return s, nil return s, nil

View file

@ -10,3 +10,13 @@ func NewNotFoundError(err error) *NotFoundError {
} }
func (e *NotFoundError) Error() string { return e.err.Error() } func (e *NotFoundError) Error() string { return e.err.Error() }
func (e *NotFoundError) Retryable() bool { return false } func (e *NotFoundError) Retryable() bool { return false }
type NonJSONResponseError struct {
err error
}
func NewNonJSONResponseError(err error) *NotFoundError {
return &NotFoundError{err}
}
func (e *NonJSONResponseError) Error() string { return e.err.Error() }
func (e *NonJSONResponseError) Retryable() bool { return false }

View file

@ -309,6 +309,10 @@ func downloadSchema(registries []registry.Registry, l jsonschema.SchemeURLLoader
if _, notfound := err.(*loader.NotFoundError); notfound { if _, notfound := err.(*loader.NotFoundError); notfound {
continue continue
} }
if _, nonJSONError := err.(*loader.NonJSONResponseError); nonJSONError {
continue
}
return nil, err return nil, err
} }

View file

@ -316,7 +316,7 @@ lastName: bar
}`), }`),
false, false,
false, false,
Error, Valid,
[]ValidationError{}, []ValidationError{},
}, },
{ {
@ -361,7 +361,7 @@ lastName: bar
[]byte(`<html>error page</html>`), []byte(`<html>error page</html>`),
true, true,
false, false,
Error, Skipped,
[]ValidationError{}, []ValidationError{},
}, },
{ {
@ -394,6 +394,9 @@ lastName: bar
return "", nil, loader.NewNotFoundError(nil) return "", nil, loader.NewNotFoundError(nil)
} }
s, err := jsonschema.UnmarshalJSON(bytes.NewReader(testCase.schemaRegistry1)) s, err := jsonschema.UnmarshalJSON(bytes.NewReader(testCase.schemaRegistry1))
if err != nil {
return "", s, loader.NewNonJSONResponseError(err)
}
return "", s, err return "", s, err
}), }),
newMockRegistry(func() (string, any, error) { newMockRegistry(func() (string, any, error) {
@ -401,6 +404,9 @@ lastName: bar
return "", nil, loader.NewNotFoundError(nil) return "", nil, loader.NewNotFoundError(nil)
} }
s, err := jsonschema.UnmarshalJSON(bytes.NewReader(testCase.schemaRegistry2)) s, err := jsonschema.UnmarshalJSON(bytes.NewReader(testCase.schemaRegistry2))
if err != nil {
return "", s, loader.NewNonJSONResponseError(err)
}
return "", s, err return "", s, err
}), }),
}, },
@ -469,6 +475,9 @@ age: not a number
regs: []registry.Registry{ regs: []registry.Registry{
newMockRegistry(func() (string, any, error) { newMockRegistry(func() (string, any, error) {
s, err := jsonschema.UnmarshalJSON(bytes.NewReader(schema)) s, err := jsonschema.UnmarshalJSON(bytes.NewReader(schema))
if err != nil {
return "", s, loader.NewNonJSONResponseError(err)
}
return "", s, err return "", s, err
}), }),
}, },