Skip to content

Commit e3bb348

Browse files
authoredFeb 27, 2023
fix: expose error instance path instead of schema path (#177)
1 parent aaecabe commit e3bb348

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed
 

‎pkg/validator/validator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func (val *v) ValidateResource(res resource.Resource) Result {
197197
if errors.As(err, &e) {
198198
for _, ve := range e.Causes {
199199
validationErrors = append(validationErrors, ValidationError{
200-
Path: ve.KeywordLocation,
200+
Path: ve.InstanceLocation,
201201
Msg: ve.Message,
202202
})
203203
}

‎pkg/validator/validator_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package validator
22

33
import (
4+
"reflect"
45
"testing"
56

67
"github.com/yannh/kubeconform/pkg/registry"
@@ -379,3 +380,58 @@ lastName: bar
379380
}
380381
}
381382
}
383+
384+
func TestValidationErrors(t *testing.T) {
385+
rawResource := []byte(`
386+
kind: name
387+
apiVersion: v1
388+
firstName: foo
389+
age: not a number
390+
`)
391+
392+
schema := []byte(`{
393+
"title": "Example Schema",
394+
"type": "object",
395+
"properties": {
396+
"kind": {
397+
"type": "string"
398+
},
399+
"firstName": {
400+
"type": "string"
401+
},
402+
"lastName": {
403+
"type": "string"
404+
},
405+
"age": {
406+
"description": "Age in years",
407+
"type": "integer",
408+
"minimum": 0
409+
}
410+
},
411+
"required": ["firstName", "lastName"]
412+
}`)
413+
414+
expectedErrors := []ValidationError{
415+
{Path: "", Msg: "missing properties: 'lastName'"},
416+
{Path: "/age", Msg: "expected integer, but got string"},
417+
}
418+
419+
val := v{
420+
opts: Opts{
421+
SkipKinds: map[string]struct{}{},
422+
RejectKinds: map[string]struct{}{},
423+
},
424+
schemaCache: nil,
425+
schemaDownload: downloadSchema,
426+
regs: []registry.Registry{
427+
newMockRegistry(func() (string, []byte, error) {
428+
return "", schema, nil
429+
}),
430+
},
431+
}
432+
433+
got := val.ValidateResource(resource.Resource{Bytes: rawResource})
434+
if !reflect.DeepEqual(expectedErrors, got.ValidationErrors) {
435+
t.Errorf("Expected %+v, got %+v", expectedErrors, got.ValidationErrors)
436+
}
437+
}

0 commit comments

Comments
 (0)
Please sign in to comment.