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 results

See original GitHub issue

Is your feature request related to a problem? Please describe.

When parsing an excel row an object value can be int, double or string.

A transform validator can determine whether the object is of an allowed type, and if so if it is truly an integer.

Once that processing is done ValidationResult is returned and if the object is valid, the transform needs to be performed again, since the ValidationResult does not contain the result.

Describe the solution you’d like

TTransformed? TransformedResult { get; } could be added to ValidationResult or a type that derives from it such as TransformedValidationResult<T>

Describe alternatives you’ve considered

The alternative to avoiding the transformation is to generate a list of ValidationFailures outside the FluentValidation library.

Additional Context

No response

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Jul 6, 2021

I don’t think setting context.RootContextData helps, since it is not available in the ValidationResult instance.

It doesn’t need to be on the result - if you instantiate the context directly before calling Validate, you can access the context again after validation has been completed.

It would seem to me that after a successful validation, the user is likely to need the int again.

If the user needs the int outside of the validator, then it should be parsed/transformed outside of the validator, eg through a surrogate property (or some other solution)

0reactions
virzakcommented, Jul 6, 2021

Seems to me that this should be a common issue using transformations. In the documentation example, the string is converted to int. It would seem to me that after a successful validation, the user is likely to need the int again.

public record MyObject (string SomeStringProperty);

public class TransformValidator : AbstractValidator<MyObject>
{
    public TransformValidator()
    {
        Transform(x => x.SomeStringProperty, StringToNullableInt).GreaterThan(10);
    }
    public static int? StringToNullableInt(string value)
      => int.TryParse(value, out int val) ? val : null;
}

class Caller
{
    void Validate()
    {
        var myObject = new MyObject("abc");

        var result = new TransformValidator().Validate(myObject);

        if (result.IsValid)
        {
            var myInt = TransformValidator.StringToNullableInt(myObject.SomeStringProperty)!.Value;
        }
    }
}

So int.TryParse is being called twice to do the exact same work. The more expensive transformations and the more instances to validate (such as a huge csv / excel spreadsheet) the slower it will be.

I don’t think setting context.RootContextData helps, since it is not available in the ValidationResult instance.

It is totally possible to have a solution where the string is only parsed once, without using the FluentValidation library, but I was just thinking it could be a common problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

See the Transform :20 Results
Transform :20 workouts are 20 minutes of fast, intense, hard work that will help sculpt your glutes, chisel your abs, strengthen and define...
Read more >
My Transform :20 Step Workout Review Including 6 Week ...
Here's my final review of the Transform :20 workout program from Shaun T. Included are my real life 6 week before and after...
Read more >
Transform Behaviors, Transform Results!: Identifying and ...
It proposes a behavioral framework to suit each particular lens. This book begins with reasons most continuous improvement programs fail to deliver the...
Read more >
Transform 20: Mom Bod Edition (In-depth Review and ...
Transform 20 is a Beachbody fitness program created and instructed by Shaun T. It is touted as a Transformation process emphasizing fitness, ...
Read more >
Transform poller results in the SolarWinds Platform
Click Transform Results, and click Next to acknowledge examples of transformations. Type the name and description for the transformation, and click Next.
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