servicestack - 如何在 ResponseStatus.Meta 中包含 ValidationError.CustomState?
问题描述
我的一个验证器中有以下代码:
RuleFor(foo => foo)
.Must(foo => foo != null)
.WithState(bar => new { Baz, Qux });
然后在我的服务中,我有这个:
var validationResult = new FooValidator().Validate(req.Foo);
if (!validationResult.IsValid)
{
throw validationResult.ToException();
}
我认为 ValidationError 的 CustomState 将包含在 ResponseStatus.Meta 属性中。然而,事实并非如此。
有没有一种干净的方法可以做到这一点?
解决方案
以前 ServiceStack 仅支持维护一个字符串字典状态,该状态将在 ResponseStatus错误字段 Meta
字典下序列化,例如:
RuleFor(foo => foo)
.Must(foo => foo != null)
.WithState(bar => new Dictionary<string,string> { ["Baz"] = "Qux" });
将在错误字段 Meta
字典中序列化,例如:
response.Errors[0].Meta //= ["Bar"] = "Qux"
这是首选方法,因为在您的 CustomState 中配置的相同将在字典Dictionary<string,string>
中按原样序列化。Meta
否则,我刚刚添加了对匿名对象的支持,因此您现在可以使用:
RuleFor(foo => foo)
.Must(foo => foo != null)
.WithState(bar => new { Baz = "Qux" });
这将导致相同的序列化响应 DTO。
此更改从 v5.9.3+ 开始可用,现在可在 MyGet 上使用。
推荐阅读
- ruby-on-rails - 在哪里将自定义 javascript 代码放在 rails 6 中?
- javascript - 根据数据中的参数值渲染组件
- android - Retrofit + coroutines:使用 Gson 进行自定义错误处理
- android - 使用 MeditorLiveData 和协程对 ViewModel 进行单元测试
- laravel - laravel db 回滚是否仅适用于打开的事务?
- xml - 尝试生成实体时出现 Doctrine Invalid Mapping File 异常
- regex - Ruby 正则表达式允许 A-Za-z0-9
- javascript - 有没有办法用 asin/acos/atan 函数 JavaScript 获得弧度?
- c# - 将 ienumerable 转换为类对象
- android - “警告:API 'variant.getMergeResources()' 已过时,已替换为 'variant.getMergeResourcesProvider()'。”