FluentValidation / FluentValidation

A popular .NET validation library for building strongly-typed validation rules.
https://fluentvalidation.net
Apache License 2.0
8.93k stars 1.19k forks source link

I would like to know what type of rule failed #1442

Closed risogolo closed 3 years ago

risogolo commented 4 years ago

Is your feature request related to a problem? Please describe. I would like to know what type of rule failed

Describe the solution you'd like Id like to be able to get from ValidationFailure object what validation rule (Type) failed, so for example validationFailure.RuleName will say GreaterThanOrEqualTo or NotEmpty, custom ones etc...

Describe alternatives you've considered Either each predfined rule will have its name or I will be also able to define custom name

JeremySkinner commented 4 years ago

Hi, take a look at the ErrorCode property on each failure - this will contain the type name (unless it’s been overridden by calling WithErrorCode)

risogolo commented 4 years ago

Hi, take a look at the ErrorCode property on each failure - this will contain the type name (unless it’s been overridden by calling WithErrorCode)

Unfortunately Im using WithErrorCode for something else, actually for setting a code, Id like to have message, rulename, code

JeremySkinner commented 4 years ago

That’s the standard way of retrieving the type name, if you’re overriding this then there’s no other built in way of retrieving this - you’d have to use something like WithState to explicitly set a value manually.

risogolo commented 4 years ago

yep, but this was not asking for help but feature reqest :) will you consider this?

JeremySkinner commented 4 years ago

I’ll think about it but it isn’t something I’m keen on. We already explicitly provide this functionality - the error code - and my gut reaction is I’m not keen on duplicating this.

If you’re choosing to override it then that’s your prerogative, but if it’s that important then I would suggest not overriding it, and stick with the standard error codes.

You asked for: “ Either each predfined rule will have its name or I will be also able to define custom name”. This is literally the ErrorCode feature.

But give me some time to think about if further during the week.

risogolo commented 4 years ago

I have a requirement to lod and also return the name/type of valiadation rule that has been broken, Currenlty I have

RuleFor(x => x.DateRangeFilter.DateFrom.Date).GreaterThan(x => x.DateRangeFilter.DateTo.Date).WithMessage("Date from field is greater then date to").WithErrorCode("XXX-XX-410); and I dlike to know that the rule was "GreaterThan" becoouse I want log something like

Object, rule GreaterThan argument 2020-11-12 and also message and code

JeremySkinner commented 4 years ago

You could do this by storing “XXX-XX-410” in a call to WithState, and then leaving the original error code unaffected.

uchagani commented 4 years ago

Is WithState available inside of IAsyncActionFilter? I'm trying to get at this value from inside one of my filters.

JeremySkinner commented 4 years ago

@uchagani is available on the ValidationFailure instances that you receive after calling Validate manually. If you're using MVC's automatic validation, then this will not be available. If you'd like to discuss this further please open a separate issue as this isn't related to this issue here. Thanks.

AuthorProxy commented 3 years ago

Will be useful to improve unit testing messages too, to understand where it was failed because now you can only see a message

FluentValidation.TestHelper.ValidationTestException
Expected no validation errors for property TotalDays
Validation Errors:
[0]: ...

and also append expected value and the actual (where it is possible to do) value to the error message, because when you autogenerating values for models at unit test, it is impossible to understand which value was provided as input