question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DisplayName should be consistent between `RuleFor.ForEach` and `RuleForEach`

See original GitHub issue

System Details

  • FluentValidation version: 8.4

Issue Description

https://github.com/JeremySkinner/FluentValidation/blob/2eb493886b3f2f630f6c82ae7fc1baf6f34ec496/src/FluentValidation/Validators/PropertyValidatorContext.cs#L31

        public class Foo
        {
            public List<int> Numbers { get; set; }
        }

        [Fact]
        public void Should_show_property_name()
        {
            var validator = new InlineValidator<Foo>();
            validator.RuleFor(x => x.Numbers).ForEach(x => x.GreaterThan(10));

            validator.ShouldHaveValidationErrorFor(x => x.Numbers, new List<int>() {2})
                     .WithErrorMessage("'Numbers[0]' must be greater than '10'.");
            
            validator = new InlineValidator<Foo>();
            validator.RuleForEach(x => x.Numbers).GreaterThan(10);

            validator.ShouldHaveValidationErrorFor(x => x.Numbers, new List<int>() {2})
                     .WithErrorMessage("'Numbers[0]' must be greater than '10'.");

        }

maybe because https://github.com/JeremySkinner/FluentValidation/blob/2eb493886b3f2f630f6c82ae7fc1baf6f34ec496/src/FluentValidation/DefaultValidatorExtensions.cs#L1091 using a InlineValidator<IEnumerable<TElement>>() with x=>x lambda, so the MemberInfo will be resolved as null.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Sep 17, 2019

Thanks for the updated example. This is technically the correct behaviour as the display name is the always the name of the property being validated. In this case, you’re not validating a property, so it will correctly be an empty string and you’re expected to provide a name using WithName.

However I agree that this isn’t particularly intuitive or useful so will look at the implications of changing this for a future version.

0reactions
JeremySkinnercommented, Apr 30, 2021

Implemented in 40dbb78 and will be part of 10.2.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FluentValidation/Changelog.txt at main
... 28 May 2021 Display name generation should be consistent between RuleForEach and RuleFor.ForEach (#1231) Allow AddFluentValidation to be called directly ...
Read more >
Overriding the Message — FluentValidation documentation
You can override the default error message for a validator by calling the WithMessage method on a validator definition: RuleFor(customer => customer.
Read more >
c# - Fluent Validation: RuleForEach and also require the ...
Right now my best guess is to write two rules (where Terms is IReadOnlyCollection<TermData> : RuleFor(x => x.Terms).NotEmpty(); RuleForEach(x => ...
Read more >
FluentValidation/FluentValidation 10.2.1 on GitHub
Fix display name generation should be consistent between RuleForEach and RuleFor.ForEach (#1231); Allow AddFluentValidation to be called directly on ...
Read more >
Combat display name spoofing : r/exchangeserver
Figure I need to rule saying if the sender is outside the organization and the recipient is inside the organization, but not sure...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found