Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OnFailureCreated callback #2120

Merged
merged 1 commit into from Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/FluentValidation.Tests/OnFailureHookTester.cs
@@ -0,0 +1,43 @@
#region License
// Copyright (c) .NET Foundation and contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// The latest version of this file can be found at https://github.com/FluentValidation/FluentValidation
#endregion

namespace FluentValidation.Tests;

using Xunit;

public class OnFailureHookTester {

[Fact]
public void Runs_hook_when_failure_created() {
try {
ValidatorOptions.Global.OnFailureCreated = (failure, context, propertyValue, rule, component) => {
failure.PropertyName = "Foo";
return failure;
};

var validator = new InlineValidator<Person>();
validator.RuleFor(x => x.Surname).NotNull();
var result = validator.Validate(new Person());
result.Errors[0].PropertyName.ShouldEqual("Foo");
}
finally {
ValidatorOptions.Global.OnFailureCreated = null;
}

}
}
4 changes: 4 additions & 0 deletions src/FluentValidation/Internal/RuleBase.cs
Expand Up @@ -325,6 +325,10 @@ public string GetDisplayName(ValidationContext<T> context)
failure.CustomState = component.CustomStateProvider(context, value);
}

if (ValidatorOptions.Global.OnFailureCreated != null) {
failure = ValidatorOptions.Global.OnFailureCreated(failure, context, value, this, component);
}

return failure;
}
}
7 changes: 7 additions & 0 deletions src/FluentValidation/ValidatorOptions.cs
Expand Up @@ -20,10 +20,12 @@ namespace FluentValidation;

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq.Expressions;
using System.Reflection;
using Internal;
using Resources;
using Results;
using Validators;

/// <summary>
Expand Down Expand Up @@ -198,6 +200,11 @@ public class ValidatorConfiguration {
set => _errorCodeResolver = value ?? DefaultErrorCodeResolver;
}

/// <summary>
/// Defines a hook that runs when a <see cref="ValidationFailure"/> is created.
/// </summary>
public Func<ValidationFailure, IValidationContext, object, IValidationRule, IRuleComponent, ValidationFailure> OnFailureCreated { get; set; }

static string DefaultPropertyNameResolver(Type type, MemberInfo memberInfo, LambdaExpression expression) {
if (expression != null) {
var chain = PropertyChain.FromExpression(expression);
Expand Down