Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relocate format checkers #161

Merged
merged 3 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 6 additions & 29 deletions code/go/internal/validator/folder_item_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"regexp"
"sync"

"github.com/elastic/package-spec/code/go/internal/validator/semantic"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: sorting imports

ve "github.com/elastic/package-spec/code/go/internal/errors"

"github.com/pkg/errors"
Expand All @@ -16,11 +18,6 @@ import (
"github.com/elastic/package-spec/code/go/internal/yamlschema"
)

const (
relativePathFormat = "relative-path"
dataStreamNameFormat = "data-stream-name"
)

type folderItemSpec struct {
Description string `yaml:"description"`
ItemType string `yaml:"type"`
Expand Down Expand Up @@ -91,13 +88,13 @@ func (s *folderItemSpec) validate(fs http.FileSystem, folderSpecPath string, ite

formatCheckersMutex.Lock()
defer func() {
unloadRelativePathFormatChecker()
unloadDataStreamNameFormatChecker()
semantic.UnloadRelativePathFormatChecker()
semantic.UnloadDataStreamNameFormatChecker()
formatCheckersMutex.Unlock()
}()

loadRelativePathFormatChecker(filepath.Dir(itemPath))
loadDataStreamNameFormatChecker(filepath.Dir(itemPath))
semantic.LoadRelativePathFormatChecker(filepath.Dir(itemPath))
semantic.LoadDataStreamNameFormatChecker(filepath.Dir(itemPath))
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
return ve.ValidationErrors{err}
Expand All @@ -113,23 +110,3 @@ func (s *folderItemSpec) validate(fs http.FileSystem, folderSpecPath string, ite
}
return errs
}

func loadRelativePathFormatChecker(currentPath string) {
gojsonschema.FormatCheckers.Add(relativePathFormat, RelativePathChecker{
currentPath: currentPath,
})
}

func unloadRelativePathFormatChecker() {
gojsonschema.FormatCheckers.Remove(relativePathFormat)
}

func loadDataStreamNameFormatChecker(currentPath string) {
gojsonschema.FormatCheckers.Add(dataStreamNameFormat, RelativePathChecker{
currentPath: filepath.Join(currentPath, "data_stream"),
})
}

func unloadDataStreamNameFormatChecker() {
gojsonschema.FormatCheckers.Remove(dataStreamNameFormat)
}
8 changes: 5 additions & 3 deletions code/go/internal/validator/folder_item_spec_errors.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package validator

import "github.com/elastic/package-spec/code/go/internal/validator/semantic"

func adjustErrorDescription(description string) string {
if description == "Does not match format '" + relativePathFormat + "'" {
if description == "Does not match format '"+semantic.RelativePathFormat+"'" {
return "relative path is invalid or target doesn't exist"
} else if description == "Does not match format '" + dataStreamNameFormat + "'" {
} else if description == "Does not match format '"+semantic.DataStreamNameFormat+"'" {
return "data stream doesn't exist"
}
return description
}
}
69 changes: 69 additions & 0 deletions code/go/internal/validator/semantic/format_checkers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package semantic

import (
"os"
"path/filepath"

"github.com/xeipuuv/gojsonschema"
)

const (
// RelativePathFormat defines the ID of the relative path format checker. This format checker
// should be used when a field's value refers to a relative filesystem path. The checker will
// ensure that the location pointed to by that relative filesystem path actually exists on
// the filesystem, relative to the file in which the field is defined.
RelativePathFormat = "relative-path"

// DataStreamNameFormat defines the ID of the data stream name format checker. This format checker
// should be used when a field's value refers to a data stream name. The checker will ensure
// that a folder with that data stream name exists on the filesystem.
DataStreamNameFormat = "data-stream-name"
)

// relativePathChecker is responsible for checking presence of the file path
type relativePathChecker struct {
currentPath string
}

// IsFormat method checks if the path exists.
func (r relativePathChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
}

path := filepath.Join(r.currentPath, asString)
_, err := os.Stat(path)
if err != nil {
return false
}
return true
}

// LoadRelativePathFormatChecker loads the relative-path format checker into the
// json-schema validation library.
func LoadRelativePathFormatChecker(currentPath string) {
gojsonschema.FormatCheckers.Add(RelativePathFormat, relativePathChecker{
currentPath: currentPath,
})
}

// UnloadRelativePathFormatChecker unloads the relative-path format checker from the
// json-schema validation library.
func UnloadRelativePathFormatChecker() {
gojsonschema.FormatCheckers.Remove(RelativePathFormat)
}

// LoadDataStreamNameFormatChecker loads the data-stream-name format checker into the
// json-schema validation library.
func LoadDataStreamNameFormatChecker(currentPath string) {
gojsonschema.FormatCheckers.Add(DataStreamNameFormat, relativePathChecker{
currentPath: filepath.Join(currentPath, "data_stream"),
})
}

// UnloadDataStreamNameFormatChecker unloads the data-stream-name format checker from the
// json-schema validation library.
func UnloadDataStreamNameFormatChecker() {
gojsonschema.FormatCheckers.Remove(DataStreamNameFormat)
}