Skip to content

Commit

Permalink
Splitting the validation result models from the main package
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuck committed Dec 11, 2020
1 parent 6f70a84 commit 7215aca
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 26 deletions.
6 changes: 6 additions & 0 deletions YuckQi.Domain.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.30804.86
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YuckQi.Domain", "src\YuckQi.Domain\YuckQi.Domain.csproj", "{D1B3FB54-0212-41AE-AB29-30AD97269375}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YuckQi.Domain.Validation", "src\YuckQi.Domain.Validation\YuckQi.Domain.Validation.csproj", "{A620A320-F2E9-43A7-858C-9BAF1D76CD8E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{D1B3FB54-0212-41AE-AB29-30AD97269375}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1B3FB54-0212-41AE-AB29-30AD97269375}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1B3FB54-0212-41AE-AB29-30AD97269375}.Release|Any CPU.Build.0 = Release|Any CPU
{A620A320-F2E9-43A7-858C-9BAF1D76CD8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A620A320-F2E9-43A7-858C-9BAF1D76CD8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A620A320-F2E9-43A7-858C-9BAF1D76CD8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A620A320-F2E9-43A7-858C-9BAF1D76CD8E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Linq;
using FluentValidation;

namespace YuckQi.Domain.Validation.Extensions
{
public static class AbstractValidatorExtensions
{
private const string AbstractValidatorFailedMessageId = "YQDV.01";

public static Result<T> GetResult<T>(this AbstractValidator<T> validator, T item)
{
var result = validator.Validate(item);
if (result == null)
throw new ArgumentNullException(nameof(result));

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

return new Result<T>(default, result.Errors.Select(t => new ResultDetail(ResultCode.InvalidRequestDetail, new ResultMessage(AbstractValidatorFailedMessageId, t.ErrorMessage), t.PropertyName)).ToList());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
using System.Collections.Generic;
using System.Linq;

namespace YuckQi.Domain.Application.Results
namespace YuckQi.Domain.Validation
{
public class Result
{
#region Properties

public IEnumerable<ResultDetail> Detail { get; }
public IReadOnlyCollection<ResultDetail> Detail { get; }

public bool IsValid => Detail.All(t => t.Type != ResultType.Error);

#endregion


#region Constructors

public Result(IReadOnlyCollection<ResultDetail> detail)
{
Detail = detail;
}

#endregion
}

public class Result<T> : Result
Expand All @@ -25,7 +35,7 @@ public class Result<T> : Result

#region Constructors

public Result(T payload)
public Result(T payload, IReadOnlyCollection<ResultDetail> detail = null) : base(detail)
{
Payload = payload;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace YuckQi.Domain.Application.Results
namespace YuckQi.Domain.Validation
{
public readonly struct ResultCode
{
Expand All @@ -19,6 +19,13 @@ public static implicit operator string(ResultCode resultCode)
#endregion


#region Constants

public static readonly ResultCode InvalidRequestDetail = new ResultCode("invalidRequestDetail");

#endregion


#region Constructors

public ResultCode(string code)
Expand Down
27 changes: 27 additions & 0 deletions src/YuckQi.Domain.Validation/ResultDetail.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace YuckQi.Domain.Validation
{
public class ResultDetail
{
#region Properties

public ResultCode Code { get; }
public ResultMessage Message { get; }
public string Property { get; }
public ResultType Type { get; }

#endregion


#region Constructors

public ResultDetail(ResultCode code, ResultMessage message, string property = null, ResultType type = ResultType.Error)
{
Code = code;
Message = message;
Property = property;
Type = type;
}

#endregion
}
}
23 changes: 23 additions & 0 deletions src/YuckQi.Domain.Validation/ResultMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace YuckQi.Domain.Validation
{
public readonly struct ResultMessage
{
#region Properties

public string Id { get; }
public string Text { get; }

#endregion


#region Constructors

public ResultMessage(string id, string text = null)
{
Id = id;
Text = text;
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace YuckQi.Domain.Application.Results
namespace YuckQi.Domain.Validation
{
public enum ResultType
{
Expand Down
22 changes: 22 additions & 0 deletions src/YuckQi.Domain.Validation/YuckQi.Domain.Validation.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kevin J Lambert</Authors>
<Version>0.1.1</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="9.3.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using YuckQi.Domain.Application.Abstract;
using YuckQi.Domain.Entities.Abstract;

namespace YuckQi.Domain.Application.Results
namespace YuckQi.Domain.Application.Queries.Results
{
public class Page<TEntity, TKey> : IPage where TEntity : class, IEntity<TKey> where TKey : struct
{
Expand Down
20 changes: 0 additions & 20 deletions src/YuckQi.Domain/Application/Results/ResultDetail.cs

This file was deleted.

0 comments on commit 7215aca

Please sign in to comment.