Skip to content

Commit

Permalink
Merge pull request #80 from sitkoru/skip-validation
Browse files Browse the repository at this point in the history
Skip validation
  • Loading branch information
pogromistik authored Mar 4, 2024
2 parents 456b2da + 3b023dc commit 50829a8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 16 deletions.
8 changes: 8 additions & 0 deletions src/Sitko.FluentValidation/Graph/FluentGraphValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class FluentGraphValidator : IFluentGraphValidator
private readonly ILogger<FluentGraphValidator> logger;
private readonly IOptions<FluentGraphValidatorOptions> options;
private readonly IServiceScope serviceScope;
private static readonly ConcurrentDictionary<string, bool> IgnoredPropertiesCache = new();

public FluentGraphValidator(IServiceProvider serviceProvider, ILogger<FluentGraphValidator> logger,
IOptions<FluentGraphValidatorOptions> options)
Expand Down Expand Up @@ -166,6 +167,13 @@ or char or uint or ulong or short or sbyte ||

foreach (var property in modelGraphValidationContext.Model.GetType().GetProperties())
{
var isSkippedProperty = IgnoredPropertiesCache.GetOrAdd($"{modelGraphValidationContext.Model.GetType()}_{property.Name}", _ => property.GetCustomAttributes(typeof(SkipGraphValidationAttribute), true)
.Cast<SkipGraphValidationAttribute>()
.FirstOrDefault() != null);
if (isSkippedProperty)
{
continue;
}
var propertyModel = property.GetValue(modelGraphValidationContext.Model);


Expand Down
14 changes: 12 additions & 2 deletions src/Sitko.FluentValidation/Sitko.FluentValidation.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
Expand All @@ -22,11 +22,21 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.0.0"/>
<PackageReference Include="FluentValidation" Version="11.9.0" />
<PackageReference Include="IsExternalInit" Version="1.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0"/>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' != 'net8.0' ">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.0"/>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.0.0"/>
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.0.0"/>
Expand Down
4 changes: 4 additions & 0 deletions src/Sitko.FluentValidation/SkipGraphValidationAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Sitko.FluentValidation;

[AttributeUsage(AttributeTargets.Property)]
public class SkipGraphValidationAttribute : Attribute;
4 changes: 4 additions & 0 deletions tests/Sitko.FluentValidation.Tests/Data/BarModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace Sitko.FluentValidation.Tests.Data;

Expand All @@ -7,4 +8,7 @@ public class BarModel
public Guid TestGuid { get; set; } = Guid.Empty;
public BarType Type { get; set; } = BarType.Bar;
public int Val { get; set; }

[SkipGraphValidation]
public List<FooModel> FooModels { get; set; } = new();
}
13 changes: 13 additions & 0 deletions tests/Sitko.FluentValidation.Tests/FluentGraphValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ public FluentGraphValidatorTests(ITestOutputHelper testOutputHelper) : base(test
{
}

[Fact]
public async Task ValidateSkipAttribute()
{
var scope = await GetScopeAsync();
var validator = scope.GetService<FluentGraphValidator>();

var bar = new BarModel { Val = 0, FooModels = new List<FooModel> { new() { Id = Guid.NewGuid() } } };
var foo = new FooModel { Id = Guid.NewGuid(), BarModels = new List<BarModel> { bar } };

var result = await validator.TryValidateModelAsync(foo);
result.Results.Where(r => r.Model.GetType() == typeof(FooModel)).Should().ContainSingle();
}

[Fact]
public async Task ValidateParent()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Sitko.Core.Xunit" Version="9.17.0"/>
<PackageReference Include="Sitko.Core.Xunit" Version="11.5.0" />
</ItemGroup>

<ItemGroup>
Expand Down
22 changes: 10 additions & 12 deletions tests/Sitko.FluentValidation.Tests/ValidationTestScope.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Sitko.Core.App;
using Microsoft.Extensions.Hosting;
using Sitko.FluentValidation.Graph;

namespace Sitko.FluentValidation.Tests;
Expand All @@ -11,23 +11,21 @@ namespace Sitko.FluentValidation.Tests;
[UsedImplicitly]
public class ValidationTestScope : BaseTestScope
{
protected override IServiceCollection ConfigureServices(IApplicationContext applicationContext,
IServiceCollection services, string name)
protected override IHostApplicationBuilder ConfigureServices(IHostApplicationBuilder builder, string name)
{
base.ConfigureServices(applicationContext, services, name);
services.AddFluentValidationExtensions();
services.AddValidatorsFromAssemblyContaining<FluentGraphValidatorTests>();
return services;
base.ConfigureServices(builder, name);
builder.Services.AddFluentValidationExtensions();
builder.Services.AddValidatorsFromAssemblyContaining<FluentGraphValidatorTests>();
return builder;
}
}

public class ValidationWithExcludedPrefixTestScope : ValidationTestScope
{
protected override IServiceCollection ConfigureServices(IApplicationContext applicationContext,
IServiceCollection services, string name)
protected override IHostApplicationBuilder ConfigureServices(IHostApplicationBuilder builder, string name)
{
base.ConfigureServices(applicationContext, services, name);
services.Configure<FluentGraphValidatorOptions>(options => options.NamespacePrefixes.Add("Sitko"));
return services;
base.ConfigureServices(builder, name);
builder.Services.Configure<FluentGraphValidatorOptions>(options => options.NamespacePrefixes.Add("Sitko"));
return builder;
}
}

0 comments on commit 50829a8

Please sign in to comment.