知识卡片
响应序列化前显式校验
请求校验与响应序列化可能是不对称的:书中 flask-smorest 的 arguments() 校验并反序列化请求,response() 只序列化响应。若服务端数据也可能畸形,应在输出前显式调用模式校验并处理错误;这增加计算成本,需按数据风险与性能预算决定范围。参见 [[出站响应也要按模型验证]]。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
原书出处
参考来源
- 位置:《Microservice APIs》第 6 章《Building REST APIs with Python》(源文件:_epub-src/OEBPS/Text/06.htm)
- 结论依据:原文明确对比 `arguments()` 与 `response()` 的行为,指出响应来自所谓可信区而默认未校验,又演示使用 `validate()` 得到错误字典;末段提醒校验开销与复杂模型问题。
- 原始内容:While the blueprint’s arguments() decorator validates and deserializes a request payload, the blueprint’s response() decorator doesn’t perform validation and only serializes the payload. ... validate() doesn’t raise an exception if it finds errors. Instead, it returns a dictionary with the errors, or an empty dictionary if no errors are found.