Skip to content

Commit 842a796

Browse files
authored
Fix filepath validator panic if the value is an existing dir (#1133)
## Fixes Or Enhances This PR adds an additional check for directories in the isFilePath() function.
1 parent 63657cf commit 842a796

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

baked_in.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,10 @@ func isFilePath(fl FieldLevel) bool {
15611561

15621562
field := fl.Field()
15631563

1564+
// Not valid if it is a directory.
1565+
if isDir(fl) {
1566+
return false
1567+
}
15641568
// If it exists, it obviously is valid.
15651569
// This is done first to avoid code duplication and unnecessary additional logic.
15661570
if exists = isFile(fl); exists {

validator_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5852,6 +5852,7 @@ func TestFilePathValidation(t *testing.T) {
58525852
{"valid filepath", filepath.Join("testdata", "a.go"), true},
58535853
{"invalid filepath", filepath.Join("testdata", "no\000.go"), false},
58545854
{"directory, not a filepath", "testdata" + string(os.PathSeparator), false},
5855+
{"directory", "testdata", false},
58555856
}
58565857

58575858
for _, test := range tests {

0 commit comments

Comments
 (0)