|
2 | 2 | using FluentValidation; |
3 | 3 | using FluentValidation.Results; |
4 | 4 | using System; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
5 | 7 |
|
6 | 8 | namespace ExpressValidator.QuickValidation |
7 | 9 | { |
@@ -52,6 +54,48 @@ private static ValidationResult ValidateInner<T>(T obj, Action<IRuleBuilderOptio |
52 | 54 | .BuildAndValidate(Unit.Default); |
53 | 55 | } |
54 | 56 |
|
| 57 | + /// <summary> |
| 58 | + /// Asynchronously validates the given object instance using <paramref name="action"/>. |
| 59 | + /// </summary> |
| 60 | + /// <typeparam name="T">The type of the object to validate.</typeparam> |
| 61 | + /// <param name="obj">The object to validate.</param> |
| 62 | + /// <param name="action">Action to add validators.</param> |
| 63 | + /// <param name="propName">The name of the property if the validation fails. |
| 64 | + /// If <see langword="null"/>, "Input" will be used.</param> |
| 65 | + /// <param name="onSuccessValidation">Specifies a method to execute when validation succeeds.</param> |
| 66 | + /// <param name="token">>A cancellation token to cancel validation.</param> |
| 67 | + /// <returns></returns> |
| 68 | + public static Task<ValidationResult> ValidateAsync<T>(T obj, Action<IRuleBuilderOptions<T, T>> action, string propName, Action<T> onSuccessValidation = null, CancellationToken token = default) |
| 69 | + { |
| 70 | + return ValidateInnerAsync(obj, action, propName ?? FALLBACK_PROP_NAME, onSuccessValidation, token); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Asynchronously validates the given object instance using <paramref name="action"/>. |
| 75 | + /// </summary> |
| 76 | + /// <typeparam name="T">The type of the object to validate.</typeparam> |
| 77 | + /// <param name="obj">The object to validate.</param> |
| 78 | + /// <param name="action">Action to add validators.</param> |
| 79 | + /// <param name="mode"><see cref="PropertyNameMode"/>. |
| 80 | + /// If <see cref="PropertyNameMode.Default"/>, "Input" will be used.</param> |
| 81 | + /// <param name="onSuccessValidation">Specifies a method to execute when validation succeeds.</param> |
| 82 | + /// <param name="token">>A cancellation token to cancel validation.</param> |
| 83 | + /// <returns></returns> |
| 84 | + public static Task<ValidationResult> ValidateAsync<T>(T obj, Action<IRuleBuilderOptions<T, T>> action, PropertyNameMode mode = PropertyNameMode.Default, Action<T> onSuccessValidation = null, CancellationToken token = default) |
| 85 | + { |
| 86 | + return ValidateInnerAsync(obj, action, GetPropName<T>(mode), onSuccessValidation, token); |
| 87 | + } |
| 88 | + |
| 89 | + private static Task<ValidationResult> ValidateInnerAsync<T>(T obj, Action<IRuleBuilderOptions<T, T>> action, string propName, Action<T> onSuccessValidation = null, CancellationToken token = default) |
| 90 | + { |
| 91 | + var eb = new ExpressValidatorBuilder<Unit>(); |
| 92 | + return eb.AddFunc((_) => obj, |
| 93 | + propName, |
| 94 | + onSuccessValidation) |
| 95 | + .WithAsyncValidation(action) |
| 96 | + .BuildAndValidateAsync(Unit.Default, token); |
| 97 | + } |
| 98 | + |
55 | 99 | private static string GetPropName<T>(PropertyNameMode mode) => mode == PropertyNameMode.Default ? FALLBACK_PROP_NAME : typeof(T).Name; |
56 | 100 | } |
57 | 101 | } |
0 commit comments