Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.10.0

- Introduced the `QuickValidator.ValidateAsync<T>(T, Action<IRuleBuilderOptions<T, T>>, string, Action<T>, CancellationToken)` extension method.
- Introduced the `QuickValidator.ValidateAsync<T>(T, Action<IRuleBuilderOptions<T, T>>, PropertyNameMode, Action<T>, CancellationToken)` extension method.
- Edit 'Quick Validation' README Chapter.
- Edit 'Quick Validation' NuGet README Chapter.


## 0.9.0

- Added quick validation support via `QuickValidator` and its `Validate<T>` overloads.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ExpressValidator is a library that provides the ability to validate objects usin
- Supports adding a property or field for validation.
- Verifies that a property expression is a property and a field expression is a field, and throws `ArgumentException` if it is not.
- Supports adding a `Func` that provides a value for validation.
- Provides quick validation (refers to ease of use).
- Provides quick and easy validation using the `QuickValidator`.
- Supports asynchronous validation.
- Targets .NET Standard 2.0+

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

## 🧩 Nuances Of Using The Library

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.3.7

- Update ExpressValidator nuget package.
- Split the DI extensions into a dedicated solution.
- Update Microsoft nuget packages.
- Update Microsoft NuGet packages for ExpressValidator.Extensions.DependencyInjection.Tests.


## 0.3.5

- Reduced unnecessary updates to validator parameters by listening to `IOptionsMonitor.Change` with named validation options.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.3.5</Version>
<Version>0.3.7</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Authors>Andrey Kolesnichenko</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand All @@ -15,7 +15,7 @@
<PackageTags>FluentValidation Validation DependencyInjection</PackageTags>
<Description>The ExpressValidator.Extensions.DependencyInjection package extends ExpressValidator to provide integration with Microsoft Dependency Injection.</Description>
<Copyright>Copyright 2024 Andrey Kolesnichenko</Copyright>
<AssemblyVersion>0.3.5.0</AssemblyVersion>
<AssemblyVersion>0.3.7.0</AssemblyVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
4 changes: 2 additions & 2 deletions src/ExpressValidator/ExpressValidator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.9.0</Version>
<Version>0.10.0</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Authors>Andrey Kolesnichenko</Authors>
<Description>ExpressValidator is a library that provides the ability to validate objects using the FluentValidation library, but without object inheritance from `AbstractValidator`.</Description>
Expand All @@ -15,7 +15,7 @@
<PackageIcon>ExpressValidator.png</PackageIcon>
<PackageReadmeFile>NuGet.md</PackageReadmeFile>
<PackageIconUrl />
<AssemblyVersion>0.9.0.0</AssemblyVersion>
<AssemblyVersion>0.10.0.0</AssemblyVersion>
<FileVersion>0.0.0.0</FileVersion>
</PropertyGroup>

Expand Down
44 changes: 44 additions & 0 deletions src/ExpressValidator/QuickValidation/QuickValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using FluentValidation;
using FluentValidation.Results;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ExpressValidator.QuickValidation
{
Expand Down Expand Up @@ -52,6 +54,48 @@ private static ValidationResult ValidateInner<T>(T obj, Action<IRuleBuilderOptio
.BuildAndValidate(Unit.Default);
}

/// <summary>
/// Asynchronously validates the given object instance using <paramref name="action"/>.
/// </summary>
/// <typeparam name="T">The type of the object to validate.</typeparam>
/// <param name="obj">The object to validate.</param>
/// <param name="action">Action to add validators.</param>
/// <param name="propName">The name of the property if the validation fails.
/// If <see langword="null"/>, "Input" will be used.</param>
/// <param name="onSuccessValidation">Specifies a method to execute when validation succeeds.</param>
/// <param name="token">>A cancellation token to cancel validation.</param>
/// <returns></returns>
public static Task<ValidationResult> ValidateAsync<T>(T obj, Action<IRuleBuilderOptions<T, T>> action, string propName, Action<T> onSuccessValidation = null, CancellationToken token = default)
{
return ValidateInnerAsync(obj, action, propName ?? FALLBACK_PROP_NAME, onSuccessValidation, token);
}

/// <summary>
/// Asynchronously validates the given object instance using <paramref name="action"/>.
/// </summary>
/// <typeparam name="T">The type of the object to validate.</typeparam>
/// <param name="obj">The object to validate.</param>
/// <param name="action">Action to add validators.</param>
/// <param name="mode"><see cref="PropertyNameMode"/>.
/// If <see cref="PropertyNameMode.Default"/>, "Input" will be used.</param>
/// <param name="onSuccessValidation">Specifies a method to execute when validation succeeds.</param>
/// <param name="token">>A cancellation token to cancel validation.</param>
/// <returns></returns>
public static Task<ValidationResult> ValidateAsync<T>(T obj, Action<IRuleBuilderOptions<T, T>> action, PropertyNameMode mode = PropertyNameMode.Default, Action<T> onSuccessValidation = null, CancellationToken token = default)
{
return ValidateInnerAsync(obj, action, GetPropName<T>(mode), onSuccessValidation, token);
}

private static Task<ValidationResult> ValidateInnerAsync<T>(T obj, Action<IRuleBuilderOptions<T, T>> action, string propName, Action<T> onSuccessValidation = null, CancellationToken token = default)
{
var eb = new ExpressValidatorBuilder<Unit>();
return eb.AddFunc((_) => obj,
propName,
onSuccessValidation)
.WithAsyncValidation(action)
.BuildAndValidateAsync(Unit.Default, token);
}

private static string GetPropName<T>(PropertyNameMode mode) => mode == PropertyNameMode.Default ? FALLBACK_PROP_NAME : typeof(T).Name;
}
}
3 changes: 2 additions & 1 deletion src/ExpressValidator/docs/NuGet.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ExpressValidator is a library that provides the ability to validate objects usin
- Supports adding a property or field for validation.
- Verifies that a property expression is a property and a field expression is a field, and throws `ArgumentException` if it is not.
- Supports adding a `Func` that provides a value for validation.
- Provides quick validation (refers to ease of use).
- Provides quick and easy validation using the `QuickValidator`.
- Supports asynchronous validation.
- Targets .NET Standard 2.0+

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

## Nuances Of Using The Library

Expand Down
Loading