Skip to content

Commit f5ec9f3

Browse files
committed
Feat: Extensions.FluentValidation
1 parent 47b5c42 commit f5ec9f3

File tree

7 files changed

+98
-4
lines changed

7 files changed

+98
-4
lines changed

.github/workflows/release.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ jobs:
4949
- name: Run tests - Extensions.DependencyInjection.Generators
5050
run: dotnet run -c Release --no-restore --no-build
5151
working-directory: "tests/Tests.CodeOfChaos.Extensions.DependencyInjection.Generators"
52+
- name: Run tests - Extensions.FluentValidation
53+
run: dotnet run -c Release --no-restore --no-build
54+
working-directory: "tests/Tests.CodeOfChaos.Extensions.FluentValidation"
5255

5356
- name: Publish to NuGet
5457
env:
@@ -61,4 +64,5 @@ jobs:
6164
dotnet nuget push src/CodeOfChaos.Extensions.Serilog/bin/Release/*.nupkg --api-key $NUGET_API_KEY --source https://api.nuget.org/v3/index.json --skip-duplicate
6265
dotnet nuget push src/CodeOfChaos.Extensions.Analyzers/bin/Release/*.nupkg --api-key $NUGET_API_KEY --source https://api.nuget.org/v3/index.json --skip-duplicate
6366
dotnet nuget push src/CodeOfChaos.Extensions.DependencyInjection/bin/Release/*.nupkg --api-key $NUGET_API_KEY --source https://api.nuget.org/v3/index.json --skip-duplicate
64-
dotnet nuget push src/CodeOfChaos.Extensions.DependencyInjection.Generators/bin/Release/*.nupkg --api-key $NUGET_API_KEY --source https://api.nuget.org/v3/index.json --skip-duplicate
67+
dotnet nuget push src/CodeOfChaos.Extensions.DependencyInjection.Generators/bin/Release/*.nupkg --api-key $NUGET_API_KEY --source https://api.nuget.org/v3/index.json --skip-duplicate
68+
dotnet nuget push src/CodeOfChaos.Extensions.FluentValidation/bin/Release/*.nupkg --api-key $NUGET_API_KEY --source https://api.nuget.org/v3/index.json --skip-duplicate

CodeOfChaos.Extensions.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.CodeOfChaos.Extension
4444
EndProject
4545
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeOfChaos.Extensions.MicrosoftLogging", "src\CodeOfChaos.Extensions.MicrosoftLogging\CodeOfChaos.Extensions.MicrosoftLogging.csproj", "{76717F23-6ED9-4C3B-81AD-89C79B7D36BE}"
4646
EndProject
47+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeOfChaos.Extensions.FluentValidation", "src\CodeOfChaos.Extensions.FluentValidation\CodeOfChaos.Extensions.FluentValidation.csproj", "{6A293FAD-9E0C-4747-B2DE-AE6504E4CB4F}"
48+
EndProject
4749
Global
4850
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4951
Debug|Any CPU = Debug|Any CPU
@@ -118,6 +120,10 @@ Global
118120
{76717F23-6ED9-4C3B-81AD-89C79B7D36BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
119121
{76717F23-6ED9-4C3B-81AD-89C79B7D36BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
120122
{76717F23-6ED9-4C3B-81AD-89C79B7D36BE}.Release|Any CPU.Build.0 = Release|Any CPU
123+
{6A293FAD-9E0C-4747-B2DE-AE6504E4CB4F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
124+
{6A293FAD-9E0C-4747-B2DE-AE6504E4CB4F}.Debug|Any CPU.Build.0 = Debug|Any CPU
125+
{6A293FAD-9E0C-4747-B2DE-AE6504E4CB4F}.Release|Any CPU.ActiveCfg = Release|Any CPU
126+
{6A293FAD-9E0C-4747-B2DE-AE6504E4CB4F}.Release|Any CPU.Build.0 = Release|Any CPU
121127
EndGlobalSection
122128
GlobalSection(NestedProjects) = preSolution
123129
{26284571-0E09-4BAF-8C2B-DF87DCC1BA0B} = {8DD280D4-1E14-4D5E-AFE6-58DD8F079DCC}
@@ -138,5 +144,6 @@ Global
138144
{9FC9F294-BDB7-4479-B002-F0409E050A1E} = {9F807E1D-D361-4CAB-8FD2-39B8A4F7AFFF}
139145
{E1F41678-9171-42C9-8877-F326D878AE05} = {8DD280D4-1E14-4D5E-AFE6-58DD8F079DCC}
140146
{76717F23-6ED9-4C3B-81AD-89C79B7D36BE} = {197E72AD-DEAB-4350-AFC3-A3BB38720BF5}
147+
{6A293FAD-9E0C-4747-B2DE-AE6504E4CB4F} = {197E72AD-DEAB-4350-AFC3-A3BB38720BF5}
141148
EndGlobalSection
142149
EndGlobal

src/CodeOfChaos.Extensions.AspNetCore/LoggingOverrideExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ public static WebApplicationBuilder OverrideLoggingWithSerilog(
3434

3535
Log.Logger = loggerConfig.CreateLogger();
3636

37+
38+
// Clear default providers and setup Serilog
3739
builder.Logging.ClearProviders();
3840
builder.Logging.AddSerilog(Log.Logger);
39-
builder.Services.AddSingleton(Log.Logger);
41+
4042
builder.Services.AddHostedService<ApplicationShutdownLoggerCleanup>(); // Ensure cleanup
41-
// builder.Services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(Log.Logger));
43+
builder.Services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(Log.Logger));
4244

4345
return builder;
4446
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<!-- Main package name -->
9+
<PackageId>CodeOfChaos.Extensions.FluentValidation</PackageId>
10+
<Version>0.37.2</Version>
11+
<Authors>Anna Sas</Authors>
12+
<Description>A Library of extensions for FluentValidation</Description>
13+
<PackageProjectUrl>https://github.com/code-of-chaos/cs-code_of_chaos-extensions</PackageProjectUrl>
14+
<PackageTags>extensions linq EntityFrameworkCore</PackageTags>
15+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
16+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
17+
<IncludeSymbols>true</IncludeSymbols>
18+
<DebugType>embedded</DebugType>
19+
<PackageLicenseFile>LICENSE</PackageLicenseFile>
20+
<PackageReadmeFile>README.md</PackageReadmeFile>
21+
<PackageIcon>icon.png</PackageIcon>
22+
</PropertyGroup>
23+
24+
<ItemGroup>
25+
<PackageReference Include="FluentValidation" Version="12.0.0-preview1" />
26+
</ItemGroup>
27+
28+
<ItemGroup>
29+
<None Include="..\..\LICENSE" Pack="true" PackagePath="" Visible="false" />
30+
<None Include="README.md" Pack="true" PackagePath="" />
31+
<None Include="../../assets/icon.png" Pack="true" PackagePath="" Visible="false" />
32+
</ItemGroup>
33+
34+
</Project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using FluentValidation;
5+
using FluentValidation.Results;
6+
7+
namespace CodeOfChaos.Extensions.FluentValidation;
8+
9+
// ---------------------------------------------------------------------------------------------------------------------
10+
// Code
11+
// ---------------------------------------------------------------------------------------------------------------------
12+
public static class ValidatorExtensions {
13+
public static bool TryValidate<T>(this IValidator<T> validator, T instance, out List<ValidationFailure> failures) {
14+
ValidationResult result = validator.Validate(instance);
15+
failures = result.Errors;
16+
return result.IsValid;
17+
}
18+
19+
public static void ThrowIfInvalid<T>(this IValidator<T> validator, T instance) {
20+
ValidationResult result = validator.Validate(instance);
21+
if (!result.IsValid) throw new ValidationException(result.Errors);
22+
}
23+
24+
public static IEnumerable<string> ValidateAndGetErrorMessages<T>(this IValidator<T> validator, T instance) {
25+
ValidationResult result = validator.Validate(instance);
26+
return result.Errors.Select(e => e.ErrorMessage);
27+
}
28+
29+
public static List<ValidationFailure> ValidateAndGetErrors<T>(this IValidator<T> validator, T instance) {
30+
ValidationResult result = validator.Validate(instance);
31+
return result.Errors;
32+
}
33+
34+
public static T ValidateOrDefault<T>(this IValidator<T> validator, T instance, Func<T> defaultValueFactory) {
35+
ValidationResult result = validator.Validate(instance);
36+
return result.IsValid ? instance : defaultValueFactory();
37+
}
38+
39+
public static async ValueTask<T?> ValidateOrDefaultAsync<T>(this IValidator<T> validator, T instance, Func<ValueTask<T>>? defaultValueFactory = null) {
40+
ValidationResult result = await validator.ValidateAsync(instance);
41+
if (result.IsValid) return instance;
42+
if (defaultValueFactory is null) return default;
43+
return await defaultValueFactory();
44+
}
45+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# CodeOfChaos.Extensions.FluentValidation

src/Tools.CodeOfChaos.Extensions/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public static async Task Main(string[] args) {
2929
"CodeOfChaos.Extensions.Analyzers",
3030
"CodeOfChaos.Extensions.MicrosoftLogging",
3131
"CodeOfChaos.Extensions.DependencyInjection",
32-
"CodeOfChaos.Extensions.DependencyInjection.Generators"
32+
"CodeOfChaos.Extensions.DependencyInjection.Generators",
33+
"CodeOfChaos.Extensions.FluentValidation"
3334
);
3435
string oneLineArgs = InputHelper.ToOneLine(args).Replace("%PROJECTS%", projects);
3536

0 commit comments

Comments
 (0)