Reputation: 654
I have the following code in one of my validators:
RuleFor(foo => foo)
.Must(foo => foo != null)
.WithState(bar => new { Baz, Qux });
Then in my service I have this:
var validationResult = new FooValidator().Validate(req.Foo);
if (!validationResult.IsValid)
{
throw validationResult.ToException();
}
I thought the CustomState of the ValidationError would be included in the ResponseStatus.Meta property. However, that was not the case.
Is there a clean way I can make this happen?
Upvotes: 2
Views: 275
Reputation: 143359
Previously ServiceStack only supported maintaining a String Dictionary state which would be serialized under the ResponseStatus Error Field Meta
Dictionary, e.g:
RuleFor(foo => foo)
.Must(foo => foo != null)
.WithState(bar => new Dictionary<string,string> { ["Baz"] = "Qux" });
Which will be serialized in the Error Field Meta
dictionary, e.g:
response.Errors[0].Meta //= ["Bar"] = "Qux"
This is the preferred approach as the same Dictionary<string,string>
configured in your CustomState will be serialized as-is in the Meta
dictionary.
Otherwise I've just added support for anonymous objects so you're now able to use:
RuleFor(foo => foo)
.Must(foo => foo != null)
.WithState(bar => new { Baz = "Qux" });
Which will result in the same serialized response DTO.
This change is available from v5.9.3+ that's now available on MyGet.
Upvotes: 1