mirror of https://github.com/abpframework/abp.git
10 changed files with 369 additions and 3 deletions
@ -0,0 +1,25 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\..\common.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netstandard2.0</TargetFramework> |
|||
<AssemblyName>Volo.Abp.FluentValidation</AssemblyName> |
|||
<PackageId>Volo.Abp.FluentValidation</PackageId> |
|||
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> |
|||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
|||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
|||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
|||
<RootNamespace /> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="FluentValidation" Version="8.2.3" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" /> |
|||
<ProjectReference Include="..\Volo.Abp.Validation\Volo.Abp.Validation.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.Abp.FluentValidation |
|||
{ |
|||
public static class AbpFluentValidationCrossCuttingConcern |
|||
{ |
|||
public const string FluentValidation = "AbpFluentValidation"; |
|||
} |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.FluentValidation |
|||
{ |
|||
public class AbpFluentValidationModule : AbpModule |
|||
{ |
|||
public override void PreConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.OnRegistred(FluentValidationInterceptorRegistrar.RegisterIfNeeded); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,53 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Aspects; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.DynamicProxy; |
|||
using Volo.Abp.Validation; |
|||
|
|||
namespace Volo.Abp.FluentValidation |
|||
{ |
|||
public class FluentValidationInterceptor : AbpInterceptor, ITransientDependency |
|||
{ |
|||
private readonly IFluentValidator _fluentValidator; |
|||
|
|||
public FluentValidationInterceptor(IFluentValidator fluentValidator) |
|||
{ |
|||
_fluentValidator = fluentValidator; |
|||
} |
|||
|
|||
public override void Intercept(IAbpMethodInvocation invocation) |
|||
{ |
|||
if (AbpCrossCuttingConcerns.IsApplied(invocation.TargetObject, AbpFluentValidationCrossCuttingConcern.FluentValidation)) |
|||
{ |
|||
invocation.Proceed(); |
|||
return; |
|||
} |
|||
|
|||
Validate(invocation); |
|||
|
|||
invocation.Proceed(); |
|||
} |
|||
|
|||
public override async Task InterceptAsync(IAbpMethodInvocation invocation) |
|||
{ |
|||
if (AbpCrossCuttingConcerns.IsApplied(invocation.TargetObject, AbpFluentValidationCrossCuttingConcern.FluentValidation)) |
|||
{ |
|||
await invocation.ProceedAsync(); |
|||
return; |
|||
} |
|||
|
|||
Validate(invocation); |
|||
|
|||
await invocation.ProceedAsync(); |
|||
} |
|||
|
|||
protected virtual void Validate(IAbpMethodInvocation invocation) |
|||
{ |
|||
_fluentValidator.Validate(new MethodInvocationValidationContext( |
|||
invocation.TargetObject, |
|||
invocation.Method, |
|||
invocation.Arguments |
|||
)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,16 @@ |
|||
using FluentValidation; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.FluentValidation |
|||
{ |
|||
public static class FluentValidationInterceptorRegistrar |
|||
{ |
|||
public static void RegisterIfNeeded(IOnServiceRegistredContext context) |
|||
{ |
|||
if (typeof(IValidator).IsAssignableFrom(context.ImplementationType)) |
|||
{ |
|||
context.Interceptors.TryAdd<FluentValidationInterceptor>(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
using System.ComponentModel.DataAnnotations; |
|||
using System.Linq; |
|||
using FluentValidation; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Validation; |
|||
|
|||
namespace Volo.Abp.FluentValidation |
|||
{ |
|||
public class FluentValidator : IFluentValidator, ITransientDependency |
|||
{ |
|||
public void Validate(MethodInvocationValidationContext context) |
|||
{ |
|||
var validationResult = new AbpValidationResult(); |
|||
|
|||
foreach (var parameterValue in context.ParameterValues) |
|||
{ |
|||
if (parameterValue is IValidator validator) |
|||
{ |
|||
var result = validator.Validate(parameterValue); |
|||
if (!result.IsValid) |
|||
{ |
|||
validationResult.Errors.AddRange(result.Errors.Select(error => |
|||
new ValidationResult(error.ErrorMessage))); |
|||
} |
|||
} |
|||
} |
|||
|
|||
if (validationResult.Errors.Any()) |
|||
{ |
|||
throw new AbpValidationException( |
|||
"Method arguments are not valid! See ValidationErrors for details.", |
|||
context.Errors |
|||
); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using Volo.Abp.Validation; |
|||
|
|||
namespace Volo.Abp.FluentValidation |
|||
{ |
|||
public interface IFluentValidator |
|||
{ |
|||
void Validate(MethodInvocationValidationContext context); |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netcoreapp2.2</TargetFramework> |
|||
<AssemblyName>Volo.Abp.FluentValidation.Tests</AssemblyName> |
|||
<PackageId>Volo.Abp.FluentValidation.Tests</PackageId> |
|||
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> |
|||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
|||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
|||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
|||
<RootNamespace /> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" /> |
|||
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
|||
<ProjectReference Include="..\..\src\Volo.Abp.FluentValidation\Volo.Abp.FluentValidation.csproj" /> |
|||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,167 @@ |
|||
using System.Threading.Tasks; |
|||
using FluentValidation; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp.Autofac; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.Validation; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.FluentValidation |
|||
{ |
|||
public class ApplicationService_FluentValidation_Tests : AbpIntegratedTest<ApplicationService_FluentValidation_Tests.TestModule> |
|||
{ |
|||
private readonly IMyAppService _myAppService; |
|||
|
|||
public ApplicationService_FluentValidation_Tests() |
|||
{ |
|||
_myAppService = ServiceProvider.GetRequiredService<IMyAppService>(); |
|||
} |
|||
|
|||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
|||
{ |
|||
options.UseAutofac(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Work_Proper_With_Right_Inputs() |
|||
{ |
|||
// MyStringValue should be aaa, MyStringValue2 should be bbb. MyStringValue3 should be ccc
|
|||
var output = _myAppService.MyMethod(new MyMethodInput |
|||
{ |
|||
MyStringValue = "aaa", |
|||
MyMethodInput2 = new MyMethodInput2 |
|||
{ |
|||
MyStringValue2 = "bbb" |
|||
}, |
|||
MyMethodInput3 = new MyMethodInput3 |
|||
{ |
|||
MyStringValue3 = "ccc" |
|||
} |
|||
}); |
|||
output.ShouldBe("aaabbbccc"); |
|||
|
|||
var asyncOutput = await _myAppService.MyMethodAsync(new MyMethodInput |
|||
{ |
|||
MyStringValue = "aaa", |
|||
MyMethodInput2 = new MyMethodInput2 |
|||
{ |
|||
MyStringValue2 = "bbb" |
|||
}, |
|||
MyMethodInput3 = new MyMethodInput3 |
|||
{ |
|||
MyStringValue3 = "ccc" |
|||
} |
|||
}); |
|||
|
|||
asyncOutput.ShouldBe("aaabbbccc"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Work_With_Wrong_Inputs() |
|||
{ |
|||
// MyStringValue should be aaa, MyStringValue2 should be bbb. MyStringValue3 should be ccc
|
|||
|
|||
Assert.Throws<AbpValidationException>(() => _myAppService.MyMethod(new MyMethodInput |
|||
{ |
|||
MyStringValue = "a", |
|||
MyMethodInput2 = new MyMethodInput2 |
|||
{ |
|||
MyStringValue2 = "b" |
|||
}, |
|||
MyMethodInput3 = new MyMethodInput3 |
|||
{ |
|||
MyStringValue3 = "c" |
|||
} |
|||
})); |
|||
|
|||
await Assert.ThrowsAsync<AbpValidationException>(async () => await _myAppService.MyMethodAsync( |
|||
new MyMethodInput |
|||
{ |
|||
MyStringValue = "a", |
|||
MyMethodInput2 = new MyMethodInput2 |
|||
{ |
|||
MyStringValue2 = "b" |
|||
}, |
|||
MyMethodInput3 = new MyMethodInput3 |
|||
{ |
|||
MyStringValue3 = "c" |
|||
} |
|||
})); |
|||
} |
|||
|
|||
[DependsOn(typeof(AbpAutofacModule))] |
|||
[DependsOn(typeof(AbpFluentValidationModule))] |
|||
public class TestModule : AbpModule |
|||
{ |
|||
public override void PreConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.OnRegistred(onServiceRegistredContext => |
|||
{ |
|||
if (typeof(IMyAppService).IsAssignableFrom(onServiceRegistredContext.ImplementationType)) |
|||
{ |
|||
onServiceRegistredContext.Interceptors.TryAdd<FluentValidationInterceptor>(); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.AddType<MyAppService>(); |
|||
} |
|||
} |
|||
|
|||
public interface IMyAppService |
|||
{ |
|||
string MyMethod(MyMethodInput input); |
|||
|
|||
Task<string> MyMethodAsync(MyMethodInput input); |
|||
} |
|||
|
|||
public class MyAppService : IMyAppService, ITransientDependency |
|||
{ |
|||
public string MyMethod(MyMethodInput input) |
|||
{ |
|||
return input.MyStringValue + input.MyMethodInput2.MyStringValue2 + input.MyMethodInput3.MyStringValue3; |
|||
} |
|||
|
|||
public Task<string> MyMethodAsync(MyMethodInput input) |
|||
{ |
|||
return Task.FromResult(input.MyStringValue + input.MyMethodInput2.MyStringValue2 + |
|||
input.MyMethodInput3.MyStringValue3); |
|||
} |
|||
} |
|||
|
|||
public class MyMethodInput : AbstractValidator<MyMethodInput> |
|||
{ |
|||
public MyMethodInput() |
|||
{ |
|||
RuleFor(x => x.MyStringValue).Equal("aaa"); |
|||
RuleFor(x => x.MyMethodInput2.MyStringValue2).Equal("bbb"); |
|||
RuleFor(customer => customer.MyMethodInput3).SetValidator(new MyMethodInput3()); |
|||
} |
|||
|
|||
public string MyStringValue { get; set; } |
|||
|
|||
public MyMethodInput2 MyMethodInput2 { get; set; } |
|||
|
|||
public MyMethodInput3 MyMethodInput3 { get; set; } |
|||
} |
|||
|
|||
public class MyMethodInput2 |
|||
{ |
|||
public string MyStringValue2 { get; set; } |
|||
} |
|||
|
|||
public class MyMethodInput3 : AbstractValidator<MyMethodInput3> |
|||
{ |
|||
public MyMethodInput3() |
|||
{ |
|||
RuleFor(x => x.MyStringValue3).Equal("ccc"); |
|||
} |
|||
|
|||
public string MyStringValue3 { get; set; } |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue