File tree Expand file tree Collapse file tree 6 files changed +103
-0
lines changed
CodingFlow.FluentValidation.UnitTests
CodingFlow.FluentValidation/Validators Expand file tree Collapse file tree 6 files changed +103
-0
lines changed Original file line number Diff line number Diff line change 1+ using CodingFlow . FluentValidation . Validators ;
2+ using FluentAssertions ;
3+ using static CodingFlow . FluentValidation . Validations ;
4+
5+ namespace CodingFlow . FluentValidation . UnitTests ;
6+
7+ public class GuidValidatorTests
8+ {
9+ [ TestCase ( "019b87ad-c2c0-7a74-af4f-5fd1d80235be" ) ]
10+ [ TestCase ( "019b87adc2c07a74af4f5fd1d80235be" ) ]
11+ public void Guid_Valid ( string input )
12+ {
13+ var result = RuleFor ( input )
14+ . IsGuid ( )
15+ . Result ( ) ;
16+
17+ result . Should ( ) . BeEquivalentTo ( new ValidationResult
18+ {
19+ IsValid = true ,
20+ Errors = [ ]
21+ } ) ;
22+ }
23+
24+ [ TestCase ( "100000000-0000-0000-0000-000000000000" ) ]
25+ [ TestCase ( "0z000000-0000-0000-0000-000000000000" ) ]
26+ public void Guid_Invalid ( string input )
27+ {
28+ var result = RuleFor ( input )
29+ . IsGuid ( )
30+ . Result ( ) ;
31+
32+ result . Should ( ) . BeEquivalentTo ( new ValidationResult
33+ {
34+ IsValid = false ,
35+ Errors = [ new ( ) { Message = $ "Value '{ input } ' is not a valid GUID." } ]
36+ } ) ;
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ namespace CodingFlow . FluentValidation . Validators ;
2+
3+ public static class IsGuidValidator
4+ {
5+ /// <summary>
6+ /// Ensures the string can be parsed into a valid GUID.
7+ /// </summary>
8+ /// <typeparam name="T">Input type.</typeparam>
9+ /// <param name="validation"></param>
10+ /// <param name="input">Value to compare against.</param>
11+ /// <returns></returns>
12+ public static FluentValidation < string > IsGuid ( this FluentValidation < string > validation )
13+ {
14+ Validate ( validation ) ;
15+
16+ return validation ;
17+ }
18+
19+ private static void Validate ( FluentValidation < string > validation )
20+ {
21+ validation . Validate (
22+ validation => Guid . TryParse ( validation . Input , out var value ) ,
23+ new ValidationError ( $ "Value '{ validation . Input } ' is not a valid GUID.")
24+ ) ;
25+ }
26+ }
Original file line number Diff line number Diff line change 1515
1616 <ItemGroup >
1717 <ProjectReference Include =" ..\CodingFlow.FluentValidation.VogenExtensions\CodingFlow.FluentValidation.VogenExtensions.csproj" />
18+ <ProjectReference Include =" ..\CodingFlow.FluentValidation\CodingFlow.FluentValidation.csproj" />
1819 </ItemGroup >
1920
2021</Project >
Original file line number Diff line number Diff line change 1+ using CodingFlow . FluentValidation . Validators ;
2+ using static CodingFlow . FluentValidation . Validations ;
3+
4+ namespace Examples ;
5+
6+ internal class IsGuidBasicExample
7+ {
8+ public void Run ( )
9+ {
10+ var input = "some string" ;
11+
12+ // begin-snippet: IsGuidBasicExample
13+ RuleFor ( input )
14+ . IsGuid ( )
15+ . Result ( ) ;
16+ // end-snippet
17+ }
18+ }
Original file line number Diff line number Diff line change @@ -120,6 +120,20 @@ RuleFor(input)
120120<sup ><a href =' /Examples/MustBasicExample.cs#L12-L16 ' title =' Snippet source file ' >snippet source</a > | <a href =' #snippet-MustBasicExample ' title =' Start of snippet ' >anchor</a ></sup >
121121<!-- endSnippet -->
122122
123+ ## IsGuid Validator
124+
125+ Ensures the string can be parsed into a valid GUID.
126+
127+ <!-- snippet: IsGuidBasicExample -->
128+ <a id =' snippet-IsGuidBasicExample ' ></a >
129+ ``` cs
130+ RuleFor (input )
131+ .IsGuid ()
132+ .Result ();
133+ ```
134+ <sup ><a href =' /Examples/IsGuidBasicExample.cs#L12-L16 ' title =' Snippet source file ' >snippet source</a > | <a href =' #snippet-IsGuidBasicExample ' title =' Start of snippet ' >anchor</a ></sup >
135+ <!-- endSnippet -->
136+
123137# Customizing
124138
125139## Custom Error Messages
Original file line number Diff line number Diff line change @@ -53,6 +53,12 @@ The predicate (aka `Must`) validator allows you to provide your own validation l
5353
5454snippet: MustBasicExample
5555
56+ ## IsGuid Validator
57+
58+ Ensures the string can be parsed into a valid GUID.
59+
60+ snippet: IsGuidBasicExample
61+
5662# Customizing
5763
5864## Custom Error Messages
You can’t perform that action at this time.
0 commit comments