diff --git a/code/go/go.mod b/code/go/go.mod index 79ee18408..812e102d5 100644 --- a/code/go/go.mod +++ b/code/go/go.mod @@ -13,6 +13,4 @@ require ( github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 github.com/xeipuuv/gojsonschema v1.2.0 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c -) - -replace github.com/elastic/package-spec/code/go/internal/pkgpath => ./internal/pkgpath +) \ No newline at end of file diff --git a/code/go/internal/validator/semantic/kibana_object_ids.go b/code/go/internal/validator/semantic/kibana_object_ids.go index 311d5397d..359c97d92 100644 --- a/code/go/internal/validator/semantic/kibana_object_ids.go +++ b/code/go/internal/validator/semantic/kibana_object_ids.go @@ -1,17 +1,38 @@ package semantic import ( + "fmt" "path/filepath" + "strings" + + "github.com/elastic/package-spec/code/go/internal/validator" + + "github.com/pkg/errors" "github.com/elastic/package-spec/code/go/internal/pkgpath" ) func ValidateKibanaObjectIDs(pkgRoot string) error { filePaths := filepath.Join(pkgRoot, "kibana", "dashboard", "*.json") - _, err := pkgpath.Files(filePaths) + dashboardFiles, err := pkgpath.Files(filePaths) if err != nil { - return err + return errors.Wrap(err, "unable to find dashboard files") + } + + var errs validator.ValidationErrors + for _, dashboardFile := range dashboardFiles { + name := dashboardFile.Name() + dashboardID, err := dashboardFile.Values("$.id") + if err != nil { + return errors.Wrap(err, "unable to get dashboard ID") + } + + fileID := strings.TrimRight(name, ".json") + if fileID != dashboardID { + err := fmt.Errorf("dashboard file '%s' defines non-matching ID '%s'", name, dashboardID) + errs = append(errs, err) + } } - return nil + return errs } diff --git a/code/go/internal/validator/spec.go b/code/go/internal/validator/spec.go index 25fd437b8..a7cec0025 100644 --- a/code/go/internal/validator/spec.go +++ b/code/go/internal/validator/spec.go @@ -5,6 +5,8 @@ import ( "path" "strconv" + "github.com/elastic/package-spec/code/go/internal/validator/semantic" + "github.com/Masterminds/semver/v3" "github.com/pkg/errors" "github.com/rakyll/statik/fs" @@ -54,5 +56,11 @@ func (s Spec) ValidatePackage(pkg Package) ValidationErrors { return errs } - return rootSpec.validate(pkg.Name, pkg.RootPath) + // Syntactic validations + errs = rootSpec.validate(pkg.Name, pkg.RootPath) + + // Semantic validations + errs = append(errs, semantic.ValidateKibanaObjectIDs(pkg.RootPath)...) + + return errs }