-
Notifications
You must be signed in to change notification settings - Fork 72
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
ycombinator
merged 3 commits into
elastic:master
from
ycombinator:relocate-format-checkers
Apr 27, 2021
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: sorting imports