Question

I'm using ServiceStack FluentValidation for validating DTOs.

I know that I can customize the error message by using

public class HeaderItemValidator : AbstractValidator<HeaderItem>
{
    public HeaderItemValidator()
    {
        RuleFor(h => h.MCode).GreaterThan(0).WithMessage("Gotcha");
        ...

The point is that I would like to put the content of the validation inside a special object that partially include some of the request DTO data and a fixed error that I want the client to receive.

Can I do that?

Was it helpful?

Solution

You can do this by creating your own validation filter.

In you AppHost Plugins Configuration:

public override void Configure(Funq.Container container)
{
    ...
    // Add the Error Response Filter
    Plugins.Add(new ValidationFeature { ErrorResponseFilter = ValidationErrorFilter });
    ...

Add to you AppHost:

// Return your own validation result format here
public static object ValidationErrorFilter(ValidationResult validationResult, object errorDto)
{
    // Loop through the validationResult create your own custom error response object.
    // return your response object 
}

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top