Skip to content

Commit 20805f6

Browse files
authored
Stop validating output of closed channel in Validate (#265)
Currently, Validate and ValidateWithContext always returns a result with status `Empty` and a `missing 'kind' key` error as the final item in the returned slice. This is because ValidateWithContext currently will parse the output of `resourcesChan`, even when the context is finished and we get back a default `Resource` struct. This PR modifies the code to skip validating this case.
1 parent 9627dd1 commit 20805f6

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

pkg/validator/validator.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,9 @@ func (val *v) ValidateWithContext(ctx context.Context, filename string, r io.Rea
223223
for {
224224
select {
225225
case res, ok := <-resourcesChan:
226-
validationResults = append(validationResults, val.ValidateResource(res))
227-
if !ok {
226+
if ok {
227+
validationResults = append(validationResults, val.ValidateResource(res))
228+
} else {
228229
resourcesChan = nil
229230
}
230231

pkg/validator/validator_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package validator
22

33
import (
4+
"bytes"
5+
"io"
46
"reflect"
57
"testing"
68

@@ -435,3 +437,65 @@ age: not a number
435437
t.Errorf("Expected %+v, got %+v", expectedErrors, got.ValidationErrors)
436438
}
437439
}
440+
441+
func TestValidateFile(t *testing.T) {
442+
inputData := []byte(`
443+
kind: name
444+
apiVersion: v1
445+
firstName: bar
446+
lastName: qux
447+
---
448+
kind: name
449+
apiVersion: v1
450+
firstName: foo
451+
`)
452+
453+
schema := []byte(`{
454+
"title": "Example Schema",
455+
"type": "object",
456+
"properties": {
457+
"kind": {
458+
"type": "string"
459+
},
460+
"firstName": {
461+
"type": "string"
462+
},
463+
"lastName": {
464+
"type": "string"
465+
}
466+
},
467+
"required": ["firstName", "lastName"]
468+
}`)
469+
470+
val := v{
471+
opts: Opts{
472+
SkipKinds: map[string]struct{}{},
473+
RejectKinds: map[string]struct{}{},
474+
},
475+
schemaCache: nil,
476+
schemaDownload: downloadSchema,
477+
regs: []registry.Registry{
478+
newMockRegistry(func() (string, []byte, error) {
479+
return "", schema, nil
480+
}),
481+
},
482+
}
483+
484+
gotStatuses := []Status{}
485+
gotValidationErrors := []ValidationError{}
486+
for _, got := range val.Validate("test-file", io.NopCloser(bytes.NewReader(inputData))) {
487+
gotStatuses = append(gotStatuses, got.Status)
488+
gotValidationErrors = append(gotValidationErrors, got.ValidationErrors...)
489+
}
490+
491+
expectedStatuses := []Status{Valid, Invalid}
492+
expectedValidationErrors := []ValidationError{
493+
{Path: "", Msg: "missing properties: 'lastName'"},
494+
}
495+
if !reflect.DeepEqual(expectedStatuses, gotStatuses) {
496+
t.Errorf("Expected %+v, got %+v", expectedStatuses, gotStatuses)
497+
}
498+
if !reflect.DeepEqual(expectedValidationErrors, gotValidationErrors) {
499+
t.Errorf("Expected %+v, got %+v", expectedValidationErrors, gotValidationErrors)
500+
}
501+
}

0 commit comments

Comments
 (0)