CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

Add validation msg to Validate Func

Open micheleissa opened this issue 6 years ago • 12 comments

Closes #1198

  • Add a way to associate an exception message with Validate lambda.
  • Add Unit test to above functionality.

micheleissa avatar Mar 17 '19 05:03 micheleissa

wondering if it is okay to use FluentAssertion lib

Does Assert.ThrowsException not work in this case?

I think instead of a string message, it should probably be a Func<string, string> so the user can use the field that failed validation in the message if they want. I was thinking if the whole row would be valuable, but that data should be passed along as part of the exception that is thrown.

JoshClose avatar Mar 20 '19 02:03 JoshClose

Something like this:

public virtual new MemberMap<TClass, TMember> Validate(Func<string, bool> validateExpression, Func<string, string> validateMessageExpression)
{
	Data.ValidateExpression = (Expression<Func<string, bool>>)(field => validateExpression(field));
	Data.ValidateMessageExpression = (Expression<Func<string, string>>)(field => validateMessageExpression(field));

	return this;
}

JoshClose avatar Mar 20 '19 02:03 JoshClose

Does Assert.ThroeException not work in this case?

Assert.ThrowException doesn't allow for thrown message inspection so it can't be asserted and that's why I used FluentAssertion I could do it with an ExtensionMethod but Ifind the lib is a more natural approach. I don't see a use case for func<string,string> The field is already a member of The exception itself and can be read/used. What would be a use case ? Can you give me an example?

micheleissa avatar Mar 20 '19 04:03 micheleissa

I would think people would naturally want to put it in their message.

$"Validation failed: Field '{field}' cannot contain spaces."

JoshClose avatar Mar 20 '19 18:03 JoshClose

I would think people would naturally want to put it in their message.

$"Validation failed: Field '{field}' cannot contain spaces."

I'll see what I can do, I'm trying now to figure out how to invoke that expression. But what would the expression achieve? like from your example the expression will execute and if evaluated not to true be passed along to the exception constructor like I did originally or something different?

micheleissa avatar Mar 20 '19 22:03 micheleissa

@JoshClose
I did the change. Could you take a look? Thanks.

micheleissa avatar Mar 31 '19 05:03 micheleissa

bump?? @JoshClose

micheleissa avatar Jun 01 '19 20:06 micheleissa

This could make life much easier for me. What's the status of this @micheleissa and @JoshClose?

cwadrupldijjit avatar Oct 25 '19 19:10 cwadrupldijjit

It'll still be at least a few weeks. I'm very swamped with work.

JoshClose avatar Oct 25 '19 22:10 JoshClose

Hi all, this feature would be super useful for me for too. Do you have any updates @JoshClose? I know a good software engineer is a busy software engineer.

dylanbudgen avatar Aug 26 '21 09:08 dylanbudgen

I'm looking for a resolution to this as well. Any word?

larkydoo avatar Nov 18 '21 21:11 larkydoo

As a work around, you can throw your own Exception type with its own message instead of returning false in your validation. That will get cauight and wrapped by CsvHelper in a CsvHelperException which will contain the context and the inner exception will be your exception, Catch the CsfHelperException and use the inner exception to get your message.

simplifirepyroneil avatar Dec 03 '21 18:12 simplifirepyroneil