mirror of
https://github.com/yannh/kubeconform.git
synced 2026-04-10 15:04:17 +00:00
Try to expose JSON paths (#173)
* Try to expose JSON paths * update validationErrors format in json output * Add test to JSON output with validationError
This commit is contained in:
parent
9860cde144
commit
563e1db94c
3 changed files with 107 additions and 13 deletions
|
|
@ -3,6 +3,7 @@ package validator
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
|
|
@ -26,11 +27,21 @@ const (
|
|||
Empty // resource is empty. Note: is triggered for files starting with a --- separator.
|
||||
)
|
||||
|
||||
type ValidationError struct {
|
||||
Path string `json:"path"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
func (ve *ValidationError) Error() string {
|
||||
return ve.Msg
|
||||
}
|
||||
|
||||
// Result contains the details of the result of a resource validation
|
||||
type Result struct {
|
||||
Resource resource.Resource
|
||||
Err error
|
||||
Status Status
|
||||
Resource resource.Resource
|
||||
Err error
|
||||
Status Status
|
||||
ValidationErrors []ValidationError
|
||||
}
|
||||
|
||||
// Validator exposes multiple methods to validate your Kubernetes resources.
|
||||
|
|
@ -181,7 +192,23 @@ func (val *v) ValidateResource(res resource.Resource) Result {
|
|||
|
||||
err = schema.Validate(r)
|
||||
if err != nil {
|
||||
return Result{Resource: res, Status: Invalid, Err: fmt.Errorf("problem validating schema. Check JSON formatting: %s", err)}
|
||||
validationErrors := []ValidationError{}
|
||||
var e *jsonschema.ValidationError
|
||||
if errors.As(err, &e) {
|
||||
for _, ve := range e.Causes {
|
||||
validationErrors = append(validationErrors, ValidationError{
|
||||
Path: ve.KeywordLocation,
|
||||
Msg: ve.Message,
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
return Result{
|
||||
Resource: res,
|
||||
Status: Invalid,
|
||||
Err: fmt.Errorf("problem validating schema. Check JSON formatting: %s", err),
|
||||
ValidationErrors: validationErrors,
|
||||
}
|
||||
}
|
||||
|
||||
return Result{Resource: res, Status: Valid}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue