Closed risogolo closed 3 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)
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
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.
yep, but this was not asking for help but feature reqest :) will you consider this?
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.
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
You could do this by storing “XXX-XX-410” in a call to WithState, and then leaving the original error code unaffected.
Is WithState
available inside of IAsyncActionFilter
? I'm trying to get at this value from inside one of my filters.
@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.
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
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