1+ using FluentValidation ;
2+ using FluentValidation . Results ;
3+ using System . Diagnostics . CodeAnalysis ;
4+
5+ namespace SnapshotIt . FluentValidation
6+ {
7+ /// <summary>
8+ /// FluentValidation extensions for SnapshotIt capturing functionality
9+ /// </summary>
10+ public static class CaptureValidationExtensions
11+ {
12+ /// <summary>
13+ /// Posts an object to the snapshot collection with validation using FluentValidation
14+ /// </summary>
15+ /// <typeparam name="T">The type of object to validate and post</typeparam>
16+ /// <param name="snapshot">The snapshot instance</param>
17+ /// <param name="input">The object to validate and post</param>
18+ /// <param name="validator">The FluentValidation validator to use</param>
19+ /// <exception cref="ValidationException">Thrown when validation fails</exception>
20+ public static void PostWithValidation < [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicProperties ) ] T > (
21+ this ISnapshot snapshot ,
22+ T input ,
23+ IValidator < T > validator )
24+ {
25+ var validationResult = validator . Validate ( input ) ;
26+
27+ if ( ! validationResult . IsValid )
28+ {
29+ throw new ValidationException ( validationResult . Errors ) ;
30+ }
31+
32+ snapshot . Post ( input ) ;
33+ }
34+
35+ /// <summary>
36+ /// Posts an object to the snapshot collection with validation using FluentValidation asynchronously
37+ /// </summary>
38+ /// <typeparam name="T">The type of object to validate and post</typeparam>
39+ /// <param name="snapshot">The snapshot instance</param>
40+ /// <param name="input">The object to validate and post</param>
41+ /// <param name="validator">The FluentValidation validator to use</param>
42+ /// <exception cref="ValidationException">Thrown when validation fails</exception>
43+ public static async Task PostWithValidationAsync < [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicProperties ) ] T > (
44+ this ISnapshot snapshot ,
45+ T input ,
46+ IValidator < T > validator )
47+ {
48+ var validationResult = await validator . ValidateAsync ( input ) ;
49+
50+ if ( ! validationResult . IsValid )
51+ {
52+ throw new ValidationException ( validationResult . Errors ) ;
53+ }
54+
55+ await snapshot . PostAsync ( input ) ;
56+ }
57+
58+ /// <summary>
59+ /// Posts multiple objects to the snapshot collection with validation using FluentValidation asynchronously
60+ /// </summary>
61+ /// <typeparam name="T">The type of objects to validate and post</typeparam>
62+ /// <param name="snapshot">The snapshot instance</param>
63+ /// <param name="values">The objects to validate and post</param>
64+ /// <param name="validator">The FluentValidation validator to use</param>
65+ /// <exception cref="ValidationException">Thrown when validation fails for any object</exception>
66+ public static async Task PostWithValidationAsync < [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicProperties ) ] T > (
67+ this ISnapshot snapshot ,
68+ T [ ] values ,
69+ IValidator < T > validator )
70+ {
71+ var validationTasks = values . Select ( value => validator . ValidateAsync ( value ) ) ;
72+ var validationResults = await Task . WhenAll ( validationTasks ) ;
73+
74+ var allErrors = validationResults
75+ . Where ( result => ! result . IsValid )
76+ . SelectMany ( result => result . Errors )
77+ . ToList ( ) ;
78+
79+ if ( allErrors . Count > 0 )
80+ {
81+ throw new ValidationException ( allErrors ) ;
82+ }
83+
84+ await snapshot . PostAsync ( values ) ;
85+ }
86+
87+ /// <summary>
88+ /// Validates an object using FluentValidation without posting to snapshot
89+ /// </summary>
90+ /// <typeparam name="T">The type of object to validate</typeparam>
91+ /// <param name="snapshot">The snapshot instance</param>
92+ /// <param name="input">The object to validate</param>
93+ /// <param name="validator">The FluentValidation validator to use</param>
94+ /// <returns>ValidationResult containing validation outcome and errors</returns>
95+ public static ValidationResult Validate < T > (
96+ this ISnapshot snapshot ,
97+ T input ,
98+ IValidator < T > validator )
99+ {
100+ return validator . Validate ( input ) ;
101+ }
102+
103+ /// <summary>
104+ /// Validates an object using FluentValidation without posting to snapshot asynchronously
105+ /// </summary>
106+ /// <typeparam name="T">The type of object to validate</typeparam>
107+ /// <param name="snapshot">The snapshot instance</param>
108+ /// <param name="input">The object to validate</param>
109+ /// <param name="validator">The FluentValidation validator to use</param>
110+ /// <returns>ValidationResult containing validation outcome and errors</returns>
111+ public static Task < ValidationResult > ValidateAsync < T > (
112+ this ISnapshot snapshot ,
113+ T input ,
114+ IValidator < T > validator )
115+ {
116+ return validator . ValidateAsync ( input ) ;
117+ }
118+ }
119+ }
0 commit comments