Skip to content

Commit

Permalink
fix: expose error instance path instead of schema path (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
levenleven authored Feb 27, 2023
1 parent aaecabe commit e3bb348
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
Expand Down
56 changes: 56 additions & 0 deletions pkg/validator/validator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validator

import (
"reflect"
"testing"

"github.com/yannh/kubeconform/pkg/registry"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit e3bb348

Please sign in to comment.