mirror of https://github.com/abpframework/abp.git
committed by
GitHub
11 changed files with 414 additions and 16 deletions
@ -0,0 +1,24 @@ |
|||||
|
<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.Validation\Volo.Abp.Validation.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,41 @@ |
|||||
|
using System; |
||||
|
using FluentValidation; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.FluentValidation |
||||
|
{ |
||||
|
public class AbpFluentValidationConventionalRegistrar : DefaultConventionalRegistrar |
||||
|
{ |
||||
|
public override void AddType(IServiceCollection services, Type type) |
||||
|
{ |
||||
|
if (typeof(IValidator).IsAssignableFrom(type)) |
||||
|
{ |
||||
|
var dtoType = GetFirstGenericArgumentOrNull(type, 1); |
||||
|
if (dtoType != null) |
||||
|
{ |
||||
|
var serverType = typeof(IValidator<>).MakeGenericType(dtoType); |
||||
|
var serviceDescriptor = ServiceDescriptor.Describe(serverType, type, ServiceLifetime.Transient); |
||||
|
|
||||
|
services.Add(serviceDescriptor); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static Type GetFirstGenericArgumentOrNull(Type type, int depth) |
||||
|
{ |
||||
|
const int maxFindDepth = 8; |
||||
|
|
||||
|
if (depth >= maxFindDepth) |
||||
|
{ |
||||
|
return null; |
||||
|
} |
||||
|
if (type.IsGenericType && type.GetGenericArguments().Length >= 1) |
||||
|
{ |
||||
|
return type.GetGenericArguments()[0]; |
||||
|
} |
||||
|
|
||||
|
return GetFirstGenericArgumentOrNull(type.BaseType, depth + 1); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.Modularity; |
||||
|
using Volo.Abp.Validation; |
||||
|
|
||||
|
namespace Volo.Abp.FluentValidation |
||||
|
{ |
||||
|
[DependsOn(typeof(AbpValidationModule))] |
||||
|
public class AbpFluentValidationModule : AbpModule |
||||
|
{ |
||||
|
public override void PreConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
context.Services.AddConventionalRegistrar(new AbpFluentValidationConventionalRegistrar()); |
||||
|
} |
||||
|
|
||||
|
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
Configure<AbpValidationOptions>(options => |
||||
|
{ |
||||
|
options.ValidationContributor.Add<FluentMethodInvocationValidator>(); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,47 @@ |
|||||
|
using System; |
||||
|
using System.ComponentModel.DataAnnotations; |
||||
|
using System.Linq; |
||||
|
using FluentValidation; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Validation; |
||||
|
|
||||
|
namespace Volo.Abp.FluentValidation |
||||
|
{ |
||||
|
public class FluentMethodInvocationValidator : IMethodInvocationValidator, ITransientDependency |
||||
|
{ |
||||
|
private readonly IServiceProvider _serviceProvider; |
||||
|
|
||||
|
public FluentMethodInvocationValidator(IServiceProvider serviceProvider) |
||||
|
{ |
||||
|
_serviceProvider = serviceProvider; |
||||
|
} |
||||
|
|
||||
|
public void Validate(MethodInvocationValidationContext context) |
||||
|
{ |
||||
|
var validationResult = new AbpValidationResult(); |
||||
|
|
||||
|
foreach (var parameterValue in context.ParameterValues) |
||||
|
{ |
||||
|
var serverType = typeof(IValidator<>).MakeGenericType(parameterValue.GetType()); |
||||
|
|
||||
|
if (_serviceProvider.GetService(serverType) 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 |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,7 +1,7 @@ |
|||||
namespace Volo.Abp.Validation |
namespace Volo.Abp.Validation |
||||
{ |
{ |
||||
public interface IMethodInvocationValidator |
public interface IMethodInvocationValidator |
||||
{ |
{ |
||||
void Validate(MethodInvocationValidationContext context); |
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,206 @@ |
|||||
|
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" |
||||
|
} |
||||
|
})); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void NotValidateMyMethod_Test() |
||||
|
{ |
||||
|
var output = _myAppService.NotValidateMyMethod(new MyMethodInput4 |
||||
|
{ |
||||
|
MyStringValue4 = "444" |
||||
|
}); |
||||
|
|
||||
|
output.ShouldBe("444"); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
[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<ValidationInterceptor>(); |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
context.Services.AddType<MyAppService>(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public interface IMyAppService |
||||
|
{ |
||||
|
string MyMethod(MyMethodInput input); |
||||
|
|
||||
|
Task<string> MyMethodAsync(MyMethodInput input); |
||||
|
|
||||
|
string NotValidateMyMethod(MyMethodInput4 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 string NotValidateMyMethod(MyMethodInput4 input) |
||||
|
{ |
||||
|
return input.MyStringValue4; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class MyMethodInput |
||||
|
{ |
||||
|
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 |
||||
|
{ |
||||
|
|
||||
|
public string MyStringValue3 { get; set; } |
||||
|
} |
||||
|
|
||||
|
public class MyMethodInput4 |
||||
|
{ |
||||
|
public string MyStringValue4 { get; set; } |
||||
|
} |
||||
|
|
||||
|
public class MyMethodInputValidator : AbstractValidator<MyMethodInput> |
||||
|
{ |
||||
|
public MyMethodInputValidator() |
||||
|
{ |
||||
|
RuleFor(x => x.MyStringValue).Equal("aaa"); |
||||
|
RuleFor(x => x.MyMethodInput2.MyStringValue2).Equal("bbb"); |
||||
|
RuleFor(customer => customer.MyMethodInput3).SetValidator(new MyMethodInput3Validator()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class MethodInputBaseValidator : AbstractValidator<MyMethodInput3> |
||||
|
{ |
||||
|
public MethodInputBaseValidator() |
||||
|
{ |
||||
|
RuleFor(x => x.MyStringValue3).NotNull(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class MyMethodInput3Validator : MethodInputBaseValidator |
||||
|
{ |
||||
|
public MyMethodInput3Validator() |
||||
|
{ |
||||
|
RuleFor(x => x.MyStringValue3).Equal("ccc"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue