fix: expose error instance path instead of schema path (#177)

This commit is contained in:
Aleksey Levenstein 2023-02-27 17:16:00 +02:00 committed by GitHub
parent aaecabe0b7
commit e3bb34851d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 1 deletions

View file

@ -197,7 +197,7 @@ func (val *v) ValidateResource(res resource.Resource) Result {
if errors.As(err, &e) {
for _, ve := range e.Causes {
validationErrors = append(validationErrors, ValidationError{
Path: ve.KeywordLocation,
Path: ve.InstanceLocation,
Msg: ve.Message,
})
}

View file

@ -1,6 +1,7 @@
package validator
import (
"reflect"
"testing"
"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)
}
}