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 property resolver for include properties #2003

Merged
merged 3 commits into from
Sep 11, 2022
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
31 changes: 30 additions & 1 deletion src/FluentValidation.Tests/NameResolutionPluggabilityTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class NameResolutionPluggabilityTester : IDisposable {

var error = validator.Validate(new Person { Address = new Address() }).Errors.Single();
error.PropertyName.ShouldEqual("Address.Country");

}

[Fact]
Expand All @@ -43,6 +42,36 @@ public class NameResolutionPluggabilityTester : IDisposable {
}
}

[Fact]
public void ShouldHaveValidationError_Should_support_custom_propertynameresolver_with_include_properties() {
try {
ValidatorOptions.Global.PropertyNameResolver = (type, prop, expr) => "foo";
var validator = new TestValidator() {
v => v.RuleFor(x => x.Surname).NotNull()
};
validator.TestValidate(new Person(), strategy => strategy.IncludeProperties(x => x.Surname)).ShouldHaveValidationErrorFor(x => x.Surname);
}
finally {
ValidatorOptions.Global.PropertyNameResolver = null;
}
}

[Fact]
public void ShouldHaveValidationError_Should_support_custom_propertynameresolver_with_include_properties_and_nested_properties() {
try {
ValidatorOptions.Global.PropertyNameResolver = (type, prop, expr) => "foo";
var validator = new TestValidator() {
v => v.RuleFor(x => x.Address.Line1).NotNull()
};
validator.TestValidate(new Person {
Address = new Address()
}, strategy => strategy.IncludeProperties(x => x.Address.Line1)).ShouldHaveValidationErrorFor(x => x.Address.Line1);
}
finally {
ValidatorOptions.Global.PropertyNameResolver = null;
}
}

public void Dispose() {
ValidatorOptions.Global.PropertyNameResolver = null;
}
Expand Down
6 changes: 3 additions & 3 deletions src/FluentValidation/Internal/MemberNameValidatorSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ public class MemberNameValidatorSelector : IValidatorSelector {
}

private static string MemberFromExpression<T>(Expression<Func<T, object>> expression) {
var chain = PropertyChain.FromExpression(expression);
var propertyName = ValidatorOptions.Global.PropertyNameResolver(typeof(T), expression.GetMember(), expression);

if (chain.Count == 0) {
if (string.IsNullOrEmpty(propertyName)) {
throw new ArgumentException($"Expression '{expression}' does not specify a valid property or field.");
}

return chain.ToString();
return propertyName;
sergeivalko marked this conversation as resolved.
Show resolved Hide resolved
}
}