diff --git a/src/pkg/packager/lint/lint.go b/src/pkg/packager/lint/lint.go index 5f30558c5a..add6a7eb01 100644 --- a/src/pkg/packager/lint/lint.go +++ b/src/pkg/packager/lint/lint.go @@ -47,11 +47,12 @@ func Validate(createOpts types.ZarfCreateOptions) (*Validator, error) { lintComponents(&validator, &createOpts) lintPkg(&validator) - if validator.jsonSchema, err = getSchemaFile(); err != nil { + jsonSchema, err := getSchemaFile() + if err != nil { return nil, err } - if err = validateSchema(&validator); err != nil { + if err = validateSchema(&validator, jsonSchema); err != nil { return nil, err } @@ -270,8 +271,8 @@ func makeFieldPathYqCompat(field string) string { return fmt.Sprintf(".%s", wrappedField) } -func validateSchema(validator *Validator) error { - schemaLoader := gojsonschema.NewBytesLoader(validator.jsonSchema) +func validateSchema(validator *Validator, jsonSchema []byte) error { + schemaLoader := gojsonschema.NewBytesLoader(jsonSchema) documentLoader := gojsonschema.NewGoLoader(validator.untypedZarfPackage) result, err := gojsonschema.Validate(schemaLoader, documentLoader) diff --git a/src/pkg/packager/lint/lint_test.go b/src/pkg/packager/lint/lint_test.go index 049f6dd4d8..3065370706 100644 --- a/src/pkg/packager/lint/lint_test.go +++ b/src/pkg/packager/lint/lint_test.go @@ -33,6 +33,13 @@ components: path: 123123 ` +const noCompPkg = ` +kind: ZarfInitConfig +metadata: + name: init + description: Testing bad yaml +` + const goodZarfPackage = ` x-name: &name good-zarf-package @@ -70,22 +77,31 @@ func TestValidateSchema(t *testing.T) { t.Run("validate schema success", func(t *testing.T) { unmarshalledYaml := readAndUnmarshalYaml[interface{}](t, goodZarfPackage) - validator := Validator{untypedZarfPackage: unmarshalledYaml, jsonSchema: getZarfSchema(t)} - err := validateSchema(&validator) + validator := Validator{untypedZarfPackage: unmarshalledYaml} + err := validateSchema(&validator, getZarfSchema(t)) require.NoError(t, err) require.Empty(t, validator.findings) }) t.Run("validate schema fail", func(t *testing.T) { unmarshalledYaml := readAndUnmarshalYaml[interface{}](t, badZarfPackage) - validator := Validator{untypedZarfPackage: unmarshalledYaml, jsonSchema: getZarfSchema(t)} - err := validateSchema(&validator) + validator := Validator{untypedZarfPackage: unmarshalledYaml} + err := validateSchema(&validator, getZarfSchema(t)) require.NoError(t, err) config.NoColor = true require.Equal(t, "Additional property not-path is not allowed", validator.findings[0].String()) require.Equal(t, "Invalid type. Expected: string, given: integer", validator.findings[1].String()) }) + t.Run("validate schema fail", func(t *testing.T) { + unmarshalledYaml := readAndUnmarshalYaml[interface{}](t, noCompPkg) + validator := Validator{untypedZarfPackage: unmarshalledYaml} + err := validateSchema(&validator, getZarfSchema(t)) + require.NoError(t, err) + config.NoColor = true + require.Equal(t, "components is required", validator.findings[0].String()) + }) + t.Run("Template in component import success", func(t *testing.T) { unmarshalledYaml := readAndUnmarshalYaml[types.ZarfPackage](t, goodZarfPackage) validator := Validator{typedZarfPackage: unmarshalledYaml} diff --git a/src/pkg/packager/lint/validator.go b/src/pkg/packager/lint/validator.go index 2f79ed5a3b..550ca4c897 100644 --- a/src/pkg/packager/lint/validator.go +++ b/src/pkg/packager/lint/validator.go @@ -49,7 +49,6 @@ func (vm validatorMessage) String() string { // Validator holds the warnings/errors and messaging that we get from validation type Validator struct { findings []validatorMessage - jsonSchema []byte typedZarfPackage types.ZarfPackage untypedZarfPackage interface{} baseDir string