Skip to content

Commit ed9c790

Browse files
committed
1 parent 6c3307e commit ed9c790

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

baked_in.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/sha256"
77
"encoding/hex"
88
"encoding/json"
9+
"errors"
910
"fmt"
1011
"io/fs"
1112
"net"
@@ -1663,23 +1664,22 @@ func isFilePath(fl FieldLevel) bool {
16631664
return false
16641665
}
16651666
if _, err = os.Stat(field.String()); err != nil {
1666-
switch t := err.(type) {
1667-
case *fs.PathError:
1668-
if t.Err == syscall.EINVAL {
1669-
// It's definitely an invalid character in the filepath.
1670-
return false
1671-
}
1672-
// It could be a permission error, a does-not-exist error, etc.
1673-
// Out-of-scope for this validation, though.
1674-
return true
1675-
default:
1667+
var pathErr *fs.PathError
1668+
if !errors.As(err, &pathErr) {
16761669
// Something went *seriously* wrong.
16771670
/*
16781671
Per https://pkg.go.dev/os#Stat:
16791672
"If there is an error, it will be of type *PathError."
16801673
*/
16811674
panic(err)
16821675
}
1676+
if errors.Is(pathErr.Err, syscall.EINVAL) {
1677+
// It's definitely an invalid character in the filepath.
1678+
return false
1679+
}
1680+
// It could be a permission error, a does-not-exist error, etc.
1681+
// Out-of-scope for this validation, though.
1682+
return true
16831683
}
16841684
}
16851685

@@ -2650,28 +2650,29 @@ func isDirPath(fl FieldLevel) bool {
26502650
return false
26512651
}
26522652
if _, err = os.Stat(field.String()); err != nil {
2653-
switch t := err.(type) {
2654-
case *fs.PathError:
2655-
if t.Err == syscall.EINVAL {
2656-
// It's definitely an invalid character in the path.
2657-
return false
2658-
}
2659-
// It could be a permission error, a does-not-exist error, etc.
2660-
// Out-of-scope for this validation, though.
2661-
// Lastly, we make sure it is a directory.
2662-
if strings.HasSuffix(field.String(), string(os.PathSeparator)) {
2663-
return true
2664-
} else {
2665-
return false
2666-
}
2667-
default:
2653+
var pathErr *fs.PathError
2654+
if !errors.As(err, &pathErr) {
26682655
// Something went *seriously* wrong.
26692656
/*
26702657
Per https://pkg.go.dev/os#Stat:
26712658
"If there is an error, it will be of type *PathError."
26722659
*/
26732660
panic(err)
26742661
}
2662+
2663+
if errors.Is(pathErr.Err, syscall.EINVAL) {
2664+
// It's definitely an invalid character in the path.
2665+
return false
2666+
}
2667+
2668+
if strings.HasSuffix(field.String(), string(os.PathSeparator)) {
2669+
// It could be a permission error, a does-not-exist error, etc.
2670+
// Out-of-scope for this validation, though.
2671+
// Lastly, we make sure it is a directory.
2672+
return true
2673+
} else {
2674+
return false
2675+
}
26752676
}
26762677
// We repeat the check here to make sure it is an explicit directory in case the above os.Stat didn't trigger an error.
26772678
if strings.HasSuffix(field.String(), string(os.PathSeparator)) {

0 commit comments

Comments
 (0)