I've just checked in a Customization Error hook that allows you to specify a custom error filter to make it easier to support this use-case in the next version of ServiceStack (v3.9.44+).
From the CustomValidationErrorTests you can now configure the ValidationFeature to use a custom error filter which will be returned instead, e.g:
public override void Configure(Container container)
{
Plugins.Add(new ValidationFeature {
ErrorResponseFilter = CustomValidationError });
container.RegisterValidators(typeof(MyValidator).Assembly);
}
public static object CustomValidationError(
ValidationResult validationResult, object errorDto)
{
var firstError = validationResult.Errors[0];
var dto = new MyCustomErrorDto {
code = firstError.ErrorCode, error = firstError.ErrorMessage };
//Ensure HTTP Clients recognize this as an HTTP Error
return new HttpError(dto, HttpStatusCode.BadRequest, dto.code, dto.error);
}
Now your HTTP Clients will receive your customized error response:
try
{
var response = "http://example.org/customerror".GetJsonFromUrl();
}
catch (Exception ex)
{
ex.GetResponseBody().Print();
//{"code":"GreaterThan","error":"'Age' must be greater than '0'."}
}
Warning: When customizing the Error Response in this way ServiceStack's typed C# clients will no longer provide typed exceptions as they expect the Error Response to contain a ResponseStatus DTO property.