Skip to content

Commit e58c8eb

Browse files
authored
refactor: Simplified FilePathRule by using RulesBase to resolve a generic catch clause
Potential fix for code scanning alert no. 35: Generic catch clause
2 parents 19d3fdc + 6ca1145 commit e58c8eb

1 file changed

Lines changed: 7 additions & 12 deletions

File tree

src/TypeGuard.Core/Rules/StringRules.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -318,24 +318,19 @@ public partial class PhoneRule(string? customMessage = null) : IValidatorRule<st
318318
/// </summary>
319319
/// <param name="mustExist">If true, validates that the file actually exists on the file system. Defaults to false.</param>
320320
/// <param name="customMessage">An optional custom error message. If not provided, a default message is used.</param>
321+
/// <exception cref="ArgumentException">Thrown when the file path is invalid.</exception>
321322
public class FilePathRule(bool mustExist = false, string? customMessage = null)
322-
: IValidatorRule<string>
323+
: RulesBase<string>(BuildPredicate(mustExist), BuildMessage(mustExist), customMessage)
323324
{
324-
/// <inheritdoc/>
325-
public bool IsValid(string value)
325+
private static Func<string, bool> BuildPredicate(bool mustExist)
326326
{
327-
try
327+
return value =>
328328
{
329329
string fullPath = Path.GetFullPath(value);
330330
return !mustExist || File.Exists(fullPath);
331-
}
332-
catch
333-
{
334-
return false;
335-
}
331+
};
336332
}
337333

338-
/// <inheritdoc/>
339-
public string ErrorMessage { get; } =
340-
customMessage ?? (mustExist ? "File path must exist" : "Input must be a valid file path");
334+
private static string BuildMessage(bool mustExist) =>
335+
mustExist ? "File path must exist" : "Input must be a valid file path";
341336
}

0 commit comments

Comments
 (0)