Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
validation for yaml format
Browse files Browse the repository at this point in the history
Signed-off-by: AdiAkhileshSingh15 <adiakhilesh.singh.cse21@itbhu.ac.in>
  • Loading branch information
AdiAkhileshSingh15 committed Aug 11, 2023
1 parent 8c34f5b commit 1755e5d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pkg/test/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"testing"
"time"

"gopkg.in/yaml.v2"

k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -738,5 +740,27 @@ func validateFieldFile(file string, dir string) error {
return fmt.Errorf("the file %s specified in the field does not exist", completeFile)
}

// Check if the file is in yaml format
err = validateYAMLFormat(completeFile)
if err != nil {
return fmt.Errorf("unable to validate yaml format and indentation for file %s", completeFile)
}

return nil
}

// validateYAMLFormat checks if the yaml file is in correct format and indentation
func validateYAMLFormat(file string) error {
data, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("failed to read file %s: %w", file, err)
}

var temp []client.Object
err = yaml.Unmarshal(data, &temp)
if err != nil {
return fmt.Errorf("failed to parse yaml file %s: %w", file, err)
}

return nil
}

0 comments on commit 1755e5d

Please sign in to comment.