Skip to content

Commit

Permalink
Added support for ValidateAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuck committed Jun 7, 2022
1 parent 64cab1e commit 7e29b88
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/YuckQi.Domain.Services/YuckQi.Domain.Services.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kevin J Lambert</Authors>
<Version>1.3.0</Version>
<Version>1.3.1</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>A .NET library for bootstrapping a domain services project.</Description>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentValidation;
using FluentValidation.Results;

Expand All @@ -10,14 +11,29 @@ public static class AbstractValidatorExtensions
{
public static Result<T> GetResult<T>(this AbstractValidator<T> validator, T item, String failedValidationMessageId)
{
var result = validator.Validate(item);
if (result == null)
throw new ArgumentNullException(nameof(result));
var validationResult = validator.Validate(item);
var result = BuildResult(validationResult, item, failedValidationMessageId);

if (result.IsValid)
return result;
}

public static async Task<Result<T>> GetResultAsync<T>(this AbstractValidator<T> validator, T item, String failedValidationMessageId)
{
var validationResult = await validator.ValidateAsync(item);
var result = BuildResult(validationResult, item, failedValidationMessageId);

return result;
}

private static Result<T> BuildResult<T>(ValidationResult validationResult, T item, String failedValidationMessageId)
{
if (validationResult == null)
throw new ArgumentNullException(nameof(validationResult));

if (validationResult.IsValid)
return new Result<T>(item);

return new Result<T>(default, GetResultDetail(result, failedValidationMessageId));
return new Result<T>(default, GetResultDetail(validationResult, failedValidationMessageId));
}

private static IReadOnlyCollection<ResultDetail> GetResultDetail(ValidationResult result, String failedValidationMessageId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kevin J Lambert</Authors>
<Version>1.3.0</Version>
<Version>1.3.1</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>A .NET library providing domain validation fundamentals.</Description>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/YuckQi.Domain/YuckQi.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kevin J Lambert</Authors>
<Version>1.3.0</Version>
<Version>1.3.1</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>A .NET library for bootstrapping a domain model project.</Description>
</PropertyGroup>
Expand Down

0 comments on commit 7e29b88

Please sign in to comment.