title: Custom response DTO in ServiceStack FluentValidation
body: <p>I am evaluating <a href="https://github.com/ServiceStack/ServiceStack/wiki/Validation#fluentvalidation-for-request-dtos" rel="noreferrer">FluentValidation in ServiceStack</a> for handling automatic validation of request DTOs:</p>
  
  <pre><code>Plugins.Add(new ValidationFeature());
  container.RegisterValidators(typeof(MyValidator).Assembly);
  </code></pre>
  
  <p>Errors are returned to the client by serializing an <code>ErrorResponse</code> DTO and may look like this:</p>
  
  <pre><code>{
      "ErrorCode": "GreaterThan",
      "Message": "'Age' must be greater than '0'.",
      "Errors": [
          {
              "ErrorCode": "GreaterThan",
              "FieldName": "Age",
              "Message": "'Age' must be greater than '0'."
          },
          {
              "ErrorCode": "NotEmpty",
              "FieldName": "Company",
              "Message": "'Company' should not be empty."
          }
      ]
  }
  </code></pre>
  
  <p>I would like to know if it is possible to return the errors using a different response DTO. For example:</p>
  
  <pre><code>{
      "code": "123",
      "error": "'Age' must be greater than '0'."
  }
  </code></pre>
  
  <p>I know that it is possible to use the validator explicitly in the service:</p>
  
  <pre><code>public MyService : Service
  {
      private readonly IValidator&lt;MyRequestDto&gt; validator;
      public MyService(IValidator&lt;MyRequestDto&gt; validator)
      {
          this.validator = validator;
      }
  
      public object Get(MyRequestDto request)
      {
          var result = this.validator.Validate(request);
          if (!result.IsValid)
          {
              throw new SomeCustomException(result);
          }
  
          ... at this stage request DTO validation has passed
      }
  }
  </code></pre>
  
  <p>But the question here is whether it is possible to have this validation error intercepted implicitly somewhere so that I can replace the response DTO and have a cleaner service:</p>
  
  <pre><code>public MyService : Service
  {
      public object Get(MyRequestDto request)
      {
          ... at this stage request DTO validation has passed
      }
  }
  </code></pre>
  
  <hr>
  
  <p>UPDATE:</p>
  
  <p>After further digging into the source code, it looks like that this is <em>burnt</em> into the <code>ValidationFeature</code> and more specifically the request filter that it registers:</p>
  
  <pre><code>public class ValidationFilters
  {
      public void RequestFilter(IHttpRequest req, IHttpResponse res, object requestDto)
      {
          var validator = ValidatorCache.GetValidator(req, requestDto.GetType());
          if (validator == null) return;
  
          var validatorWithHttpRequest = validator as IRequiresHttpRequest;
          if (validatorWithHttpRequest != null)
              validatorWithHttpRequest.HttpRequest = req;
  
          var ruleSet = req.HttpMethod;
          var validationResult = validator.Validate(
          new ValidationContext(requestDto, null, new MultiRuleSetValidatorSelector(ruleSet)));
  
          if (validationResult.IsValid) return;
  
          var errorResponse = DtoUtils.CreateErrorResponse(
              requestDto, validationResult.ToErrorResult());
  
          res.WriteToResponse(req, errorResponse);
      }
  }
  </code></pre>
  
  <p>By writing a custom validation feature I was able to achieve the desired effect. But maybe there's a more elegant way?</p>