I upgraded to the latest version of FluentValidation at this time (11.9.0) from an older version, and I noticed that now the error messages in my app contain the suffix: "Severity: Error". Leaving aside the idea, that, imo, when changes are made to a library, extra care should be taken to leave the existing behaviour unperturbed as much as possible, is it possible to turn off the addition of this suffix?
See ValidationException.
public ValidationException(string message, IEnumerable<ValidationFailure> errors, bool appendDefaultMessage)
: base(appendDefaultMessage ? $"{message} {BuildErrorMessage(errors)}" : message) {
Errors = errors;
}
/// <summary>
/// Creates a new ValidationException
/// </summary>
/// <param name="errors"></param>
public ValidationException(IEnumerable<ValidationFailure> errors) : base(BuildErrorMessage(errors)) {
Errors = errors;
}
private static string BuildErrorMessage(IEnumerable<ValidationFailure> errors) {
var arr = errors.Select(x => $"{Environment.NewLine} -- {x.PropertyName}: {x.ErrorMessage} Severity: {x.Severity.ToString()}");
return "Validation failed: " + string.Join(string.Empty, arr);
}
The BuildErrorMessage method is called from a number of constructors. I am not sure how I can avoid its call.
And one other thing. I attached some custom validation methods to the object itself that is validated. The default message displays the PropertyName followed by colon. If the x.PropertyName is null the message will contain: -- : which looks weird. On top of that, sometimes the names used in the code don't reflect concepts that make sense to the end users.
Thanks
Update: After reading the code, one way would be to extend AbstractValidator and override RaiseValidationException to call the ValidationException constructor that doesn't decorate the message:
protected override void RaiseValidationException(ValidationContext<T> context, ValidationResult result)
=> throw new ValidationException(result.Errors, false);
Unfortunately there is no constructor that takes the boolean flag, so I would need to extend the exception as well.
My solution as linqpad 7 script with FluentValidator 11.9.0 added as dependency :
It produces the output:
Now all my validators will have to inherit
AbstractValidatorEx(bulk search & replace).