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.

Transform doen't work for RuleForEach

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Jul 16, 2020

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.

0reactions
JeremySkinnercommented, Aug 8, 2020

9.1 is now available on nuget with this change.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Transform property before Fluent Validation
I need to transform my property (remove white space on a string) first before applying validation. Specifically I want to check if a...
Read more >
Problem moving javascript transform from OH1 to OH2
I' am slowly moving my OH1 setup to OH2. I have most basic things working, and have moved on to the "next level"...
Read more >
dynamic xfr - Data Management
Wanted to know about dynamic xfr where xfr is table driven. Calcuations will be stored in a table. Ab initio job should unoad...
Read more >
Transforming Values — FluentValidation documentation
As of FluentValidation 9.5, you can apply a transformation to a property value prior to validation being performed against it. For example, if...
Read more >
ATL » Rule transforming one element to several elements
I'm doing an UML to UML transformation, and I run into a problem. ... I tried that and it doesn't work, the same...
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