Skip to content

Commit 4e7622c

Browse files
authored
Merge pull request #55 from kolan72/dev
Update main before release.
2 parents 2a1c189 + e98df2f commit 4e7622c

4 files changed

Lines changed: 303 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ExpressValidator is a library that provides the ability to validate objects usin
1818
- Supports adding a property or field for validation.
1919
- Verifies that a property expression is a property and a field expression is a field, and throws `ArgumentException` if it is not.
2020
- Supports adding a `Func` that provides a value for validation.
21-
- Provides quick validation (refers to ease of use).
21+
- Provides quick and easy validation using the `QuickValidator`.
2222
- Supports asynchronous validation.
2323
- Targets .NET Standard 2.0+
2424

@@ -159,6 +159,7 @@ var result = QuickValidator.Validate(
159159
.ChildRules((v) => v.RuleFor(o => o.PercentValue1).InclusiveBetween(0, 100)),
160160
nameof(obj));
161161
```
162+
The `QuickValidator` also provides a `ValidateAsync` method for asynchronous validation.
162163

163164
## 🧩 Nuances Of Using The Library
164165

src/ExpressValidator/QuickValidation/QuickValidator.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using FluentValidation;
33
using FluentValidation.Results;
44
using System;
5+
using System.Threading;
6+
using System.Threading.Tasks;
57

68
namespace ExpressValidator.QuickValidation
79
{
@@ -52,6 +54,48 @@ private static ValidationResult ValidateInner<T>(T obj, Action<IRuleBuilderOptio
5254
.BuildAndValidate(Unit.Default);
5355
}
5456

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+
5599
private static string GetPropName<T>(PropertyNameMode mode) => mode == PropertyNameMode.Default ? FALLBACK_PROP_NAME : typeof(T).Name;
56100
}
57101
}

src/ExpressValidator/docs/NuGet.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ExpressValidator is a library that provides the ability to validate objects usin
88
- Supports adding a property or field for validation.
99
- Verifies that a property expression is a property and a field expression is a field, and throws `ArgumentException` if it is not.
1010
- Supports adding a `Func` that provides a value for validation.
11-
- Provides quick validation (refers to ease of use).
11+
- Provides quick and easy validation using the `QuickValidator`.
1212
- Supports asynchronous validation.
1313
- Targets .NET Standard 2.0+
1414

@@ -144,6 +144,7 @@ var result = QuickValidator.Validate(
144144
.ChildRules((v) => v.RuleFor(o => o.PercentValue1).InclusiveBetween(0, 100)),
145145
nameof(obj));
146146
```
147+
The `QuickValidator` also provides a `ValidateAsync` method for asynchronous validation.
147148

148149
## Nuances Of Using The Library
149150

0 commit comments

Comments
 (0)