Skip to content

Transform doen't work for RuleForEach #1450

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

Closed
KindElk opened this issue Jul 16, 2020 · 5 comments · Fixed by #1451
Closed

Transform doen't work for RuleForEach #1450

KindElk opened this issue Jul 16, 2020 · 5 comments · Fixed by #1451
Milestone

Comments

@KindElk
Copy link

KindElk commented Jul 16, 2020

Version of FluentValidation: 9.0.0

.NET Framework 4.7

Description
Given the following code (which compiles fine) I'd expect Transform will be applied for each collection member.

  class ModelWithCollection {
    public List<int> Collection { get; } = new List<int>() { 1, 2, 3, 4 };
  }

  class Validator : AbstractValidator<ModelWithCollection> {
    public Validator() {
      RuleForEach(model => model.Collection).Transform(i => i.ToString()).NotEqual("3");
    }
  }

But in runtime call new Validator().Validate(new ModelWithCollection()) thows an exception
System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.List`1[System.Int32]' to type 'System.Int32'.'
This occurs in FluentValidation.Internal.Extensions.CoerceToNonGeneric<T, TProperty>.
Obviously, Transform behaves the same way for simple property and for collection rules, so in this case it gets whole collection as input, but inner function expects collection element.

I'd expect Transfom either fail to compile in this case, or clearly state it will work with collection, or work with each separate element.

@JeremySkinner
Copy link
Member

Thanks for reporting, that looks like a bug. Ideally when used with RuleForEach it should work against the collection elements (while it would work against the whole collection when used with RuleFor). I'll look at this as part of the 9.1 release.

@JeremySkinner JeremySkinner added this to the 9.1 milestone Jul 16, 2020
@KindElk
Copy link
Author

KindElk commented Jul 16, 2020

Thank you!

@KindElk
Copy link
Author

KindElk commented Jul 16, 2020

For my case this fix works perfect, thanks a lot.
Should I close this issue or you will?

@JeremySkinner
Copy link
Member

I’ll close it once it’s merged - thanks.

JeremySkinner added a commit that referenced this issue Jul 16, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@JeremySkinner
Copy link
Member

9.1 is now available on nuget with this change.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants