- Notifications
You must be signed in to change notification settings - Fork23
fix: correct URL validation and centralize logic#421
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
38bc617
fix: correct URL validation and centralize logic
ssncferreirade3cf09
fix: go imports order
ssncferreirae46a0a7
tests: add validation URL tests
ssncferreira1907d5a
revert: parameter_test ValidDefaultWithOptions test update
ssncferreira30ad47c
test: update validation_test to use require
ssncferreira484b5b1
test: update validation_test tests
ssncferreiraFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
15 changes: 5 additions & 10 deletionsprovider/app.go
This file contains hidden or 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
57 changes: 57 additions & 0 deletionsprovider/app_test.go
This file contains hidden or 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
22 changes: 22 additions & 0 deletionsprovider/helpers/validation.go
This file contains hidden or 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,22 @@ | ||
package helpers | ||
import ( | ||
"fmt" | ||
"net/url" | ||
) | ||
// ValidateURL validates that value is a valid URL string. | ||
// Accepts empty strings, local file paths, file:// URLs, and http/https URLs. | ||
// Example: for `icon = "/icon/region.svg"`, value is `/icon/region.svg` and label is `icon`. | ||
func ValidateURL(value any, label string) ([]string, []error) { | ||
val, ok := value.(string) | ||
if !ok { | ||
return nil, []error{fmt.Errorf("expected %q to be a string", label)} | ||
} | ||
if _, err := url.Parse(val); err != nil { | ||
return nil, []error{err} | ||
} | ||
return nil, nil | ||
} |
151 changes: 151 additions & 0 deletionsprovider/helpers/validation_test.go
This file contains hidden or 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,151 @@ | ||
package helpers | ||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/require" | ||
) | ||
func TestValidateURL(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
value any | ||
label string | ||
expectError bool | ||
errorContains string | ||
}{ | ||
// Valid cases | ||
{ | ||
name: "empty string", | ||
value: "", | ||
label: "url", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "valid http URL", | ||
value: "http://example.com", | ||
label: "url", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "valid https URL", | ||
value: "https://example.com/path", | ||
label: "url", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "absolute file path", | ||
value: "/path/to/file", | ||
label: "url", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "relative file path", | ||
value: "./file.txt", | ||
label: "url", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "relative path up directory", | ||
value: "../config.json", | ||
label: "url", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "simple filename", | ||
value: "file.txt", | ||
label: "url", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "URL with query params", | ||
value: "https://example.com/search?q=test", | ||
label: "url", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "URL with fragment", | ||
value: "https://example.com/page#section", | ||
label: "url", | ||
expectError: false, | ||
}, | ||
// Various URL schemes that url.Parse accepts | ||
{ | ||
name: "file URL scheme", | ||
value: "file:///path/to/file", | ||
label: "url", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "ftp scheme", | ||
value: "ftp://files.example.com/file.txt", | ||
label: "url", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "mailto scheme", | ||
value: "mailto:user@example.com", | ||
label: "url", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "tel scheme", | ||
value: "tel:+1234567890", | ||
label: "url", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "data scheme", | ||
value: "data:text/plain;base64,SGVsbG8=", | ||
label: "url", | ||
expectError: false, | ||
}, | ||
// Invalid cases | ||
{ | ||
name: "non-string type - int", | ||
value: 123, | ||
label: "url", | ||
expectError: true, | ||
errorContains: "expected \"url\" to be a string", | ||
}, | ||
{ | ||
name: "non-string type - nil", | ||
value: nil, | ||
label: "config_url", | ||
expectError: true, | ||
errorContains: "expected \"config_url\" to be a string", | ||
}, | ||
{ | ||
name: "invalid URL with spaces", | ||
value: "http://example .com", | ||
label: "url", | ||
expectError: true, | ||
errorContains: "invalid character", | ||
}, | ||
{ | ||
name: "malformed URL", | ||
value: "http://[::1:80", | ||
label: "endpoint", | ||
expectError: true, | ||
errorContains: "missing ']'", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
warnings, errors := ValidateURL(tt.value, tt.label) | ||
if tt.expectError { | ||
require.Len(t, errors, 1, "expected an error but got none") | ||
require.Contains(t, errors[0].Error(), tt.errorContains) | ||
} else { | ||
require.Empty(t, errors, "expected no errors but got: %v", errors) | ||
} | ||
// Should always return nil for warnings | ||
require.Nil(t, warnings, "expected warnings to be nil but got: %v", warnings) | ||
}) | ||
} | ||
} |
15 changes: 5 additions & 10 deletionsprovider/metadata.go
This file contains hidden or 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
27 changes: 8 additions & 19 deletionsprovider/parameter.go
This file contains hidden or 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
28 changes: 27 additions & 1 deletionprovider/parameter_test.go
This file contains hidden or 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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.