Pergunta

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?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top