Skip to content

Commit

Permalink
fix: don't add parent failures to child results
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Jan 27, 2023
1 parent 7d8b18b commit 6cd6931
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/Sitko.FluentValidation/Graph/FluentGraphValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,13 @@ or char or uint or ulong or short or sbyte ||
var validationResult = await validator.ValidateAsync(validationContext, cancellationToken);
if (!validationResult.IsValid)
{
modelResult.Errors.AddRange(validationResult.Errors);
foreach (var validationFailure in validationResult.Errors)
{
if (!result.Contains(validationFailure))
{
modelResult.Errors.Add(validationFailure);
}
}
}
}

Expand Down Expand Up @@ -210,7 +216,7 @@ await TryValidateModelAsync(
public record GraphValidationContextOptions
{
public Func<object, bool>? NeedToValidate { get; init; }
};
}

public abstract record GraphValidationContext(GraphValidationContextOptions Options);

Expand Down
3 changes: 3 additions & 0 deletions src/Sitko.FluentValidation/Graph/ModelsValidationResult.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using FluentValidation.Results;

namespace Sitko.FluentValidation.Graph;

Expand All @@ -25,4 +26,6 @@ public override string ToString()

return errors.ToString();
}

public bool Contains(ValidationFailure validationFailure) => Results.Any(result => result.Errors.Contains(validationFailure));
}
26 changes: 20 additions & 6 deletions tests/Sitko.FluentValidation.Tests/FluentGraphValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
namespace Sitko.FluentValidation.Tests;

using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Core.Xunit;
using Data;
using FluentAssertions;
using Graph;
using Sitko.Core.Xunit;
using Sitko.FluentValidation.Graph;
using Sitko.FluentValidation.Tests.Data;
using Xunit;
using Xunit.Abstractions;

namespace Sitko.FluentValidation.Tests;

public class FluentGraphValidatorTests : BaseTest<ValidationTestScope>
{
public FluentGraphValidatorTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
Expand Down Expand Up @@ -51,6 +51,20 @@ public async Task ValidateChild()
fooResult.Errors.Should().Contain(failure => failure.PropertyName == nameof(BarModel.Val));
}

[Fact]
public async Task ValidateBoth()
{
var scope = await GetScopeAsync();
var validator = scope.GetService<FluentGraphValidator>();
var bar = new BarModel { Val = 0 };
var foo = new FooModel { Id = Guid.Empty, BarModels = new List<BarModel> { bar } };
var result = await validator.TryValidateModelAsync(foo);
result.IsValid.Should().BeFalse();
result.Results.Should().HaveCount(2);
result.Results.Where(r => r.IsValid).Should().BeEmpty();
result.Results.SelectMany(r => r.Errors).Should().HaveCount(2);
}

[Fact]
public async Task SkipChildValidation()
{
Expand Down

0 comments on commit 6cd6931

Please sign in to comment.