Browse Source

Merge pull request #2441 from kelely/dev

When using FluentValidation for validation, members in ValidationResult are missing
pull/2483/head
Halil İbrahim Kalkan 7 years ago
committed by GitHub
parent
commit
b06418f844
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      framework/src/Volo.Abp.FluentValidation/Volo/Abp/FluentValidation/FluentObjectValidationContributor.cs
  2. 22
      framework/test/Volo.Abp.FluentValidation.Tests/Volo/Abp/FluentValidation/ApplicationService_FluentValidation_Tests.cs

2
framework/src/Volo.Abp.FluentValidation/Volo/Abp/FluentValidation/FluentObjectValidationContributor.cs

@ -32,7 +32,7 @@ namespace Volo.Abp.FluentValidation
context.Errors.AddRange(
result.Errors.Select(
error =>
new ValidationResult(error.ErrorMessage)
new ValidationResult(error.ErrorMessage, new[] { error.PropertyName })
)
);
}

22
framework/test/Volo.Abp.FluentValidation.Tests/Volo/Abp/FluentValidation/ApplicationService_FluentValidation_Tests.cs

@ -1,3 +1,4 @@
using System.Linq;
using System.Threading.Tasks;
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
@ -48,7 +49,23 @@ namespace Volo.Abp.FluentValidation
{
// MyStringValue should be aaa, MyStringValue2 should be bbb. MyStringValue3 should be ccc
await Assert.ThrowsAsync<AbpValidationException>(async () => await _myAppService.MyMethodAsync(
var exception = Assert.Throws<AbpValidationException>(() => _myAppService.MyMethod(new MyMethodInput
{
MyStringValue = "a",
MyMethodInput2 = new MyMethodInput2
{
MyStringValue2 = "b"
},
MyMethodInput3 = new MyMethodInput3
{
MyStringValue3 = "c"
}
}));
exception.ValidationErrors.ShouldContain(x => x.MemberNames.Contains("MyStringValue"));
exception.ValidationErrors.ShouldContain(x => x.MemberNames.Contains("MyMethodInput2.MyStringValue2"));
exception.ValidationErrors.ShouldContain(x => x.MemberNames.Contains("MyMethodInput3.MyStringValue3"));
exception = await Assert.ThrowsAsync<AbpValidationException>(async () => await _myAppService.MyMethodAsync(
new MyMethodInput
{
MyStringValue = "a",
@ -61,6 +78,9 @@ namespace Volo.Abp.FluentValidation
MyStringValue3 = "c"
}
}));
exception.ValidationErrors.ShouldContain(x => x.MemberNames.Contains("MyStringValue"));
exception.ValidationErrors.ShouldContain(x => x.MemberNames.Contains("MyMethodInput2.MyStringValue2"));
exception.ValidationErrors.ShouldContain(x => x.MemberNames.Contains("MyMethodInput3.MyStringValue3"));
}
[Fact]

Loading…
Cancel
Save