Fluent validation with dynamic message - asp.net-mvc

I am trying to building custom validation with dynamic message in fluent validation library.
For example :
public class CreateProcessValidator : AbstractValidator<CreateProcessVM>
{
public CreateProcessValidator()
{
RuleFor(x => x.ProcessFile).Must((x,e) => IsProcessFileValid(x.ProcessFile))).WithMessage("Parse failed with error : {0}");
}
public bool IsProcessFileValid(HttpPostedFileBase file)
{
var errorMessage = "..." // pass result to validaton message ?
// logic
return false;
}
}
Is here any workaround how to pass validation result ?
Thanks

Have you tried something like this?
public class IsProcessFileValid : PropertyValidator
{
public IsProcessFileValid(): base("{ValidationMessage}") {}
protected override IsValid(PropertyValidatorContext context)
{
if (!IsProcessFileValid1(context))
context.MessageFormatter.AppendArgument("ValidationMessage",
"Custom validation message #1");
if (!IsProcessFileValid2(context))
context.MessageFormatter.AppendArgument("ValidationMessage",
"Custom validation message #2");
// ...etc
return true;
}
private bool IsProcessFileValid1(PropertyValidatorContext context)
{
// logic
return false;
}
private bool IsProcessFileValid2(PropertyValidatorContext context)
{
// logic
return false;
}
// ...etc
}
With extension method:
public static class IsProcessFileValidExtensions
{
public static IRuleBuilderOptions<T, object> MustBeValidProcessFile<T>
(this IRuleBuilder<T, object> ruleBuilder)
{
return ruleBuilder.SetValidator(new IsProcessFileValid());
}
}
... and then use it without a custom WithMessage:
public CreateProcessValidator()
{
RuleFor(x => x.ProcessFile).MustBeValidProcessFile();
}
By creating a custom PropertyValidator, you can encapsulate the default validation message within that class and make it dynamic. However you must not use the .WithMessage extension when declaring the RuleFor, because that would override the default validation message which you customized directly inside the PropertyValidator.

There's no way to do that. I would split the complex validation method you currently have into smaller methods (IsProcessFileValid1, IsProcessFileValid2, IsProcessFileValid3, ...) so that you could have more fine grained control over the error message. Also each method will be responsible for validating only once concern making them more reusable (single responsibility):
RuleFor(x => x.ProcessFile)
.Must(IsProcessFileValid1)
.WithMessage("Message 1")
.Must(IsProcessFileValid2)
.WithMessage("Message 2")
.Must(IsProcessFileValid3)
.WithMessage("Message 3");
Also notice how I simplified the lambda as the method could directly be passed to Must as argument.

Here is how I solved it. Tested with FluentValidation v8.5.0
class EmptyValidationMessage : IStringSource
{
public string ResourceName => null;
public Type ResourceType => null;
public string GetString(IValidationContext context)
{
return string.Empty;
}
public static readonly EmptyValidationMessage Instance = new EmptyValidationMessage();
}
public class MyPropValidator : PropertyValidator
{
public MyPropValidator() : base(EmptyValidationMessage.Instance)
{
}
protected override bool IsValid(PropertyValidatorContext context)
{
// if not valid
Options.ErrorMessageSource = new StaticStringSource("my message");
// you can do LanguageStringSource, LazyStringSource, LocalizedStringSource, etc
// example with localized string (https://github.com/clearwaterstream/LocalizedString.FluentValidation)
Options.ErrorMessageSource = new LocalizedStringSource("my message").InFrench("moi message");
return false;
}
}

Faced the same issue, while trying to insert exception message into WithMessage().
It worked with the method overload taking Func<T, string> messageProvider as parameter.
Here is the solution presented on the posters example (working code, FluentValidation v 9.1):
public class CreateProcessVM
{
public object ProcessFile { get; set; }
}
public class CreateProcessValidator : AbstractValidator<CreateProcessVM>
{
public CreateProcessValidator()
{
var message = "Something went wrong.";
RuleFor(x => x.ProcessFile)
.Must((x, e) => IsProcessFileValid(x.ProcessFile, out message))
// .WithMessage(message); will NOT work
.WithMessage(x => message); //Func<CreateProcessVM, string> as parameter
}
public bool IsProcessFileValid(object file, out string errorMessage)
{
errorMessage = string.Empty;
try
{
Validate(file);
return true;
}
catch (InvalidOperationException e)
{
errorMessage = e.Message;
return false;
}
}
private void Validate(object file)
{
throw new InvalidOperationException("File of type .custom is not allowed.");
}
}
And a test demonstrating that we really get the exception message in the error message:
[Fact]
public void Test()
{
var validator = new CreateProcessValidator();
var result = validator.Validate(new CreateProcessVM());
Assert.False(result.IsValid);
Assert.Equal("File of type .custom is not allowed.", result.Errors[0].ErrorMessage);
}

Related

How to customize the built-in MVC validation response format?

I'm using my own middleware to capture exceptions thrown in my API to format the response to the client. This includes things like checking for the dev env to send additional information and logging. This all works great but the built-in validation middleware responds with a different response format. I want to keep the functionality and just change what data is sent to the client and how it's formatted.
Currently it returns the default
{
"message": "Validation error(s)",
"details": [
"The value '35353535353535353535353535353535353535353535' is not valid."
]
}
You can customize the default response by using a BadResultObject in the InvalidaModelStateResponseFactory of the ApiBehaviorOptions class. As an example:
apiBehaviorOptions.InvalidModelStateResponseFactory = actionContext => {
return new BadRequestObjectResult(new {
Code = 400,
Request_Id = "Someuniqueid",
Messages = actionContext.ModelState.Values.SelectMany(x => x.Errors)
.Select(x => x.ErrorMessage)
});
Configured:
serviceCollection.PostConfigure<ApiBehaviorOptions>(apiBehaviorOptions =>
apiBehaviorOptions.InvalidModelStateResponseFactory = ...
);
Or you can send the response directly from the action you are using as well with your own custom validation error result class. For example:
public class ValidationError
{
[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
public string Field { get; }
public string Message { get; }
public ValidationError(string field, string message)
{
Field = field != string.Empty ? field : null;
Message = message;
}
}
public class ValidationResultModel
{
public string Message { get; }
public List<ValidationError> Errors { get; }
public ValidationResultModel(ModelStateDictionary modelState)
{
Message = "Validation Failed";
Errors = modelState.Keys
.SelectMany(key => modelState[key].Errors.Select(x => new
ValidationError(key, x.ErrorMessage)))
.ToList();
}
}
Then we can create our own IActionResult. Here:
public class ValidationFailedResult : ObjectResult
{
public ValidationFailedResult(ModelStateDictionary modelState)
: base(new ValidationResultModel(modelState))
{
StatusCode = StatusCodes.Status404...;
}
}
And update our ValidateModelAttribute by overriding the OnActionExecuting to perform actions before they are taken.
public class ValidateModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
if (!context.ModelState.IsValid)
{
context.Result = new ValidationFailedResult(context.ModelState);
}
}
}
Sources:
Customize automatic response on validation error
https://www.jerriepelser.com/blog/validation-response-aspnet-core-webapi/

With ASP.NET MVC, how to display errors when outside controller?

I'm trying to easily display errors in my View from anywhere in my code using :
#Html.ValidationSummary("", new { #class = "text-danger" })
Before MVC, I used :
ValidationError.Display("My error message");
And my ValidationError class looks like this:
public class ValidationError : IValidator
{
private ValidationError(string message)
{
ErrorMessage = message;
IsValid = false;
}
public string ErrorMessage { get; set; }
public bool IsValid { get; set; }
public void Validate()
{
// no action required
}
public static void Display(string message)
{
// here is the only part I would like to change ideally
var currentPage = HttpContext.Current.Handler as Page;
currentPage.Validators.Add(new ValidationError(message));
}
}
Now with MVC, to add errors, I can't use currentPage.Validators.
I need to use ModelState but my problem is that I can't access ModelState when I'm not in the Controller. I tried accessing the controller or the ModelState via HttpContext but I've not found a way to do it. Any idea ?
ModelState.AddModelError("", "My error message");
1. You can access it through ViewContext.ViewData.ModelState. Then use
#if (!ViewContext.ViewData.ModelState.IsValid)
{
<div>There are some errors</div>
}
OR
ViewData.ModelState.IsValidField("NameOfInput")
get a list of inputs:
var errors = ViewData.ModelState.Where(n => n.Value.Errors.Count > 0).ToList();
2. You can pass your model state around like this:
public class MyClass{
public static void errorMessage(ModelStateDictionary ModelState) {
if (something) ModelState.AddModelError("", "Error Message");
}
}
Use in controller:
MyClass.errorMessage(ModelState);
Use in view:
MyClass.errorMessage(ViewContext.ViewData.ModelState.IsValid);
3. ModelState via ActionFilter
public class ValidateModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.Controller.ViewData.ModelState.IsValid)
{
//Do Something
}
}
}
You can get more help from this and this links.

Get action local variables name and value OnException customized filter

Want to retrieve the variable value of action into customized filter
public class TrackError : IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
// How to get the value of X ?????????????????
}
}
Controller :
[TrackError]
public class HomeController : Controller
{
public ActionResult Index()
{
int x = 0;
throw new Exception("XYZ");
return View();
}
}
Have you tried this way.
int x = 0;
try
{
DoSomethingThatMightFail(s);
}
catch (Exception ex) when (Log(ex, "An error occurred", new[]{x,s}))
{
// this catch block will never be reached
}
...
static bool Log(Exception ex, string message, params object[] args)
{
Debug.Print(message, args);
return false;
}
Create custom exception and put whatever additional data you need into its properties. Then you catch that exception type in one of your catch blocks within your exception filter.
public class ServiceException : Exception, ISerializable
{
public WhateverType X {get;set;}
public string Message{get;set;}
public ServiceException()
{
// Add implementation.
}
public ServiceException(WhateverType x, string message)
{
this.X = x;
this.Message = message;
}
public ServiceException(string message):base(message)
{
}
public ServiceException(string message, Exception inner)
{
// Add implementation.
}
// This constructor is needed for serialization.
protected ServiceException(SerializationInfo info, StreamingContext context)
{
// Add implementation.
}
}
and then in filter:
public override void OnException(HttpActionExecutedContext context)
{
if (context.Exception is ServiceException)
{
//Here you can access (context.Exception as ServiceException).X
}
}
throw your exception like:
throw new ServiceException(X, "Your custom message gore here");

Web api HTTP 500 (Internal Server Error)

Hello I have an error 500 (internal server error) when I run the code below. My issue is that I have no trace at all of the error. It seems that visual studio is unable to catch it.
The following code returns a Candidate if I try to add pers to candidate the code fail and i get error 500. The thing is PersonAddressDescription implement AddressDescription is inheritance the problem ?
public class CheckController : ApiController
{
public Candidate Get()
{
PersonAddressDescription pers = new PersonAddressDescription();
Candidate candidate = new Candidate();
//IF I REMOVE THIS NO PROBLEM
candidate.address = pers;
return candidate;
}
}
AddressDescription class
/// <remarks/>
[System.Xml.Serialization.XmlIncludeAttribute(typeof(CompanyAddressDescription))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(PersonAddressDescription))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.17626")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.crif-online.ch/webservices/crifsoapservice/v1.00")]
public abstract partial class AddressDescription : object, System.ComponentModel.INotifyPropertyChanged {
private Location locationField;
private ContactItem[] contactItemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=0)]
public Location location {
get {
return this.locationField;
}
set {
this.locationField = value;
this.RaisePropertyChanged("location");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("contactItems", Order=1)]
public ContactItem[] contactItems {
get {
return this.contactItemsField;
}
set {
this.contactItemsField = value;
this.RaisePropertyChanged("contactItems");
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
PersonAddressDescription class that implement AddressDescription
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.17626")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.crif-online.ch/webservices/crifsoapservice/v1.00")]
public partial class PersonAddressDescription : AddressDescription {
private string firstNameField;
private string lastNameField;
private string maidenNameField;
private Sex sexField;
private bool sexFieldSpecified;
private string birthDateField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=0)]
public string firstName {
get {
return this.firstNameField;
}
set {
this.firstNameField = value;
this.RaisePropertyChanged("firstName");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=1)]
public string lastName {
get {
return this.lastNameField;
}
set {
this.lastNameField = value;
this.RaisePropertyChanged("lastName");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=2)]
public string maidenName {
get {
return this.maidenNameField;
}
set {
this.maidenNameField = value;
this.RaisePropertyChanged("maidenName");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=3)]
public Sex sex {
get {
return this.sexField;
}
set {
this.sexField = value;
this.RaisePropertyChanged("sex");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool sexSpecified {
get {
return this.sexFieldSpecified;
}
set {
this.sexFieldSpecified = value;
this.RaisePropertyChanged("sexSpecified");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=4)]
public string birthDate {
get {
return this.birthDateField;
}
set {
this.birthDateField = value;
this.RaisePropertyChanged("birthDate");
}
}
}
I suspect that the object you retrieved (addResp) contains circular references somewhere in its object graph. Circular references cannot be JSON serialized.
For example try putting the following code inside your controller to test what happens when you attempt to JSON serialize this instance:
TypeIdentifyAddressResponse addResp = ws.identifyAddress("test");
string json = JsonConvert.SerializeObject(addResp);
UPDATE:
It seems that AddressDescription is an abstract class and your actual instance is PersonAddressDescription. You need to indicate that to the serializer by using the [KnownType] attribute:
[KnownType(typeof(PersonAddressDescription))]
[KnownType(typeof(CompanyAddressDescription))]
...
public abstract partial class AddressDescription : object, System.ComponentModel.INotifyPropertyChanged {
{
...
}
As an alternative if you don't want to further pollute your (already polluted) domain models with other attributes you could also define the known type inside your WebApiConfig.cs:
config.Formatters.XmlFormatter.SetSerializer<Candidate>(
new DataContractSerializer(typeof(Candidate),
new Type[] { typeof(PersonAddressDescription) }));

How does NerdDinner's AddModelErrors work?

I'm going through the NerDinner free tutorial
http://nerddinnerbook.s3.amazonaws.com/Intro.htm
I got to somewhere in Step 5 where it says to make the code cleaner we can create an extension method. I look at the completed code and it has this to use the extension method:
catch
{
ModelState.AddModelErrors(dinner.GetRuleViolations());
return View(new DinnerFormViewModel(dinner));
}
And then this as the extension method's definition.
namespace NerdDinner.Helpers {
public static class ModelStateHelpers {
public static void AddModelErrors(this ModelStateDictionary modelState, IEnumerable<RuleViolation> errors) {
foreach (RuleViolation issue in errors) {
modelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}
}
}
}
I try to follow what the tutorial says combined with what the code contains but receive the expected error that there is no AddModelErrors method that accepts only 1 argument.
I'm obviously missing something very important here. What is it?
You need to include the helpers reference;
using NerdDinner.Helpers;
and
using NerdDinner.Models;
Then check for valid and add the errors;
if (!dinner.IsValid)
{
ModelState.AddModelErrors(dinner.GetRuleViolations());
return View(dinner);
}
You must also have a partial class for your dinner;
public partial class Dinner
{
public bool IsValid
{
get { return (GetRuleViolations().Count() == 0); }
}
public IEnumerable<RuleViolation> GetRuleViolations()
{
if (String.IsNullOrEmpty( SomeField ))
yield return new RuleViolation("Field value text is required", "SomeField");
}
partial void OnValidate(ChangeAction action)
{
if (!IsValid)
throw new ApplicationException("Rule violations prevent saving");
}
}
Don't forget the RuleViolation class;
public class RuleViolation
{
public string ErrorMessage { get; private set; }
public string PropertyName { get; private set; }
public RuleViolation(string errorMessage)
{
ErrorMessage = errorMessage;
}
public RuleViolation(string errorMessage, string propertyName)
{
ErrorMessage = errorMessage;
PropertyName = propertyName;
}
}
If you are receiving the same error message as this poster:
"'System.Web.Mvc.ModelStateDictionary' does not contain a definition for 'AddModelErrors' and no extension method 'AddModelErrors' accepting a first argument of type 'System.Web.Mvc.ModelStateDictionary' could be found (are you missing a using directive or an assembly reference?)"
You may be having this problem:
http://p2p.wrox.com/book-professional-asp-net-mvc-1-0-isbn-978-0-470-38461-9/74321-addmodalerrors-allcountries-page-87-view-data-dictionary.html#post248356

Resources