Skip to content

Commit 6ca1145

Browse files
committed
refactor: Simplified FilePathRule through RulesBase
1 parent 41ec155 commit 6ca1145

1 file changed

Lines changed: 7 additions & 28 deletions

File tree

src/TypeGuard.Core/Rules/StringRules.cs

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -318,40 +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 (ArgumentException)
333-
{
334-
return false;
335-
}
336-
catch (ArgumentNullException)
337-
{
338-
return false;
339-
}
340-
catch (PathTooLongException)
341-
{
342-
return false;
343-
}
344-
catch (NotSupportedException)
345-
{
346-
return false;
347-
}
348-
catch (IOException)
349-
{
350-
return false;
351-
}
331+
};
352332
}
353333

354-
/// <inheritdoc/>
355-
public string ErrorMessage { get; } =
356-
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";
357336
}

0 commit comments

Comments
 (0)