Skip to content

Commit

Permalink
change how schema is put down
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <AustinAbro321@gmail.com>
  • Loading branch information
AustinAbro321 committed Jul 23, 2024
1 parent fcf714c commit 467d66d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/pkg/packager/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
24 changes: 20 additions & 4 deletions src/pkg/packager/lint/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
1 change: 0 additions & 1 deletion src/pkg/packager/lint/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 467d66d

Please sign in to comment.