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

DisplayName should be consistent between RuleFor.ForEach and RuleForEach #1231

Closed
lust4life opened this issue Sep 17, 2019 · 6 comments
Closed
Milestone

Comments

@lust4life
Copy link

lust4life commented Sep 17, 2019

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.

@JeremySkinner
Copy link
Member

JeremySkinner commented Sep 17, 2019

Please can you provide the following information which was asked for in the issue template:

  • Version of FluentValidation that you're using
  • All code necesasry to reproduce the issue (if you can write a unit test that'd be even more helpful)

Thanks.

@lust4life
Copy link
Author

@JeremySkinner code updated.

@JeremySkinner JeremySkinner modified the milestones: 8.5, 9.0 Sep 17, 2019
@JeremySkinner
Copy link
Member

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.

@lust4life
Copy link
Author

😉

@ahi-el
Copy link

ahi-el commented Feb 13, 2021

@JeremySkinner Hi Jeremy, this does seem to be the correct behavior. Do you have any idea how to prevent this kind of issue?
Is there some way to hint that they should use 'WithName'?

@JeremySkinner JeremySkinner added the Status: Awaiting PR Awaiting a pull request. label Apr 15, 2021
@JeremySkinner JeremySkinner modified the milestones: Backlog, 10.2 Apr 30, 2021
@JeremySkinner
Copy link
Member

Implemented in 40dbb78 and will be part of 10.2.

@JeremySkinner JeremySkinner changed the title show PropertyName if displayName is null ? DisplayName should be consistent between RuleFor.ForEach and RuleForEach Apr 30, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants