mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-18 09:27:02 +00:00
fix: expose error instance path instead of schema path
This commit is contained in:
parent
aaecabe0b7
commit
0299e3fa4f
2 changed files with 57 additions and 1 deletions
|
|
@ -197,7 +197,7 @@ func (val *v) ValidateResource(res resource.Resource) Result {
|
||||||
if errors.As(err, &e) {
|
if errors.As(err, &e) {
|
||||||
for _, ve := range e.Causes {
|
for _, ve := range e.Causes {
|
||||||
validationErrors = append(validationErrors, ValidationError{
|
validationErrors = append(validationErrors, ValidationError{
|
||||||
Path: ve.KeywordLocation,
|
Path: ve.InstanceLocation,
|
||||||
Msg: ve.Message,
|
Msg: ve.Message,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package validator
|
package validator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/yannh/kubeconform/pkg/registry"
|
"github.com/yannh/kubeconform/pkg/registry"
|
||||||
|
|
@ -379,3 +380,58 @@ lastName: bar
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidationErrors(t *testing.T) {
|
||||||
|
rawResource := []byte(`
|
||||||
|
kind: name
|
||||||
|
apiVersion: v1
|
||||||
|
firstName: foo
|
||||||
|
age: not a number
|
||||||
|
`)
|
||||||
|
|
||||||
|
schema := []byte(`{
|
||||||
|
"title": "Example Schema",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"kind": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"firstName": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"lastName": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"age": {
|
||||||
|
"description": "Age in years",
|
||||||
|
"type": "integer",
|
||||||
|
"minimum": 0
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["firstName", "lastName"]
|
||||||
|
}`)
|
||||||
|
|
||||||
|
expectedErrors := []ValidationError{
|
||||||
|
{Path: "", Msg: "missing properties: 'lastName'"},
|
||||||
|
{Path: "/age", Msg: "expected integer, but got string"},
|
||||||
|
}
|
||||||
|
|
||||||
|
val := v{
|
||||||
|
opts: Opts{
|
||||||
|
SkipKinds: map[string]struct{}{},
|
||||||
|
RejectKinds: map[string]struct{}{},
|
||||||
|
},
|
||||||
|
schemaCache: nil,
|
||||||
|
schemaDownload: downloadSchema,
|
||||||
|
regs: []registry.Registry{
|
||||||
|
newMockRegistry(func() (string, []byte, error) {
|
||||||
|
return "", schema, nil
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
got := val.ValidateResource(resource.Resource{Bytes: rawResource})
|
||||||
|
if !reflect.DeepEqual(expectedErrors, got.ValidationErrors) {
|
||||||
|
t.Errorf("Expected %+v, got %+v", expectedErrors, got.ValidationErrors)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue