|
6 | 6 | "crypto/sha256"
|
7 | 7 | "encoding/hex"
|
8 | 8 | "encoding/json"
|
| 9 | + "errors" |
9 | 10 | "fmt"
|
10 | 11 | "io/fs"
|
11 | 12 | "net"
|
@@ -1663,23 +1664,22 @@ func isFilePath(fl FieldLevel) bool {
|
1663 | 1664 | return false
|
1664 | 1665 | }
|
1665 | 1666 | 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) { |
1676 | 1669 | // Something went *seriously* wrong.
|
1677 | 1670 | /*
|
1678 | 1671 | Per https://pkg.go.dev/os#Stat:
|
1679 | 1672 | "If there is an error, it will be of type *PathError."
|
1680 | 1673 | */
|
1681 | 1674 | panic(err)
|
1682 | 1675 | }
|
| 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 |
1683 | 1683 | }
|
1684 | 1684 | }
|
1685 | 1685 |
|
@@ -2650,28 +2650,29 @@ func isDirPath(fl FieldLevel) bool {
|
2650 | 2650 | return false
|
2651 | 2651 | }
|
2652 | 2652 | 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) { |
2668 | 2655 | // Something went *seriously* wrong.
|
2669 | 2656 | /*
|
2670 | 2657 | Per https://pkg.go.dev/os#Stat:
|
2671 | 2658 | "If there is an error, it will be of type *PathError."
|
2672 | 2659 | */
|
2673 | 2660 | panic(err)
|
2674 | 2661 | }
|
| 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 | + } |
2675 | 2676 | }
|
2676 | 2677 | // We repeat the check here to make sure it is an explicit directory in case the above os.Stat didn't trigger an error.
|
2677 | 2678 | if strings.HasSuffix(field.String(), string(os.PathSeparator)) {
|
|
0 commit comments