← 返回刚才阅读的位置

知识卡片

问题卡 · 5025.d

发送前校验请求正文

理解等级:中级

客户端提交不合法的请求正文会增加服务拒绝与返工,应在发送前检查。

稳健性原则在发送侧要求尽量保守,主动保证自己发出的消息有效。客户端发出POST、PUT或PATCH前,应检查正文能否正确解析,以及字段类型和取值是否有效。JSON/XML可使用原生schema与验证器;schema编译后用消息验证,保留状态及错误信息供调用者处理。

简单表单可先用qs.parse(formData)转成名值对象,再交给JSON Schema验证;带点号或其他嵌套命名约定的表单不能假定自动得到正确对象树。schema不可靠时应维护定制验证器,不能因已有文档就放弃实际校验。书中AJV及XML示例是历史演示,未在此执行。

关联:[[输入规则随响应更新]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 超媒体客户端 · 查看原书依据

      There are two levels of confirmation that clients need to handle: is the document well-formed, and is the document valid? Well-formed documents have the proper structure. This means they can be successfully parsed. Valid documents are not only well-formed, but both the structural elements (e.g., street_name, purchase_price) and the values of the those elements are of the right data type and within acceptable value ranges (e.g., purchase_price is a numerical value greater than zero and less than one thousand).

    参考来源

    - 位置:《RESTful Web API Patterns and Practices Cookbook》第4章《Hypermedia Clients》(来源项目book-sources:extracted/RESTful Web API Patterns and Practices Cookbook Connecting and Orchestrating Microservices and Distributed Data (Mike Amundsen)/_epub-src/OEBPS/ch04.xhtml,配方4.12) - 结论依据:原文区分well-formed与valid,并分别演示JSON、XML及简单表单的验证与嵌套命名局限。 - 原始内容:There are two levels of confirmation that clients need to handle: is the document well-formed, and is the document valid? Well-formed documents have the proper structure. This means they can be successfully parsed. Valid documents are not only well-formed, but both the structural elements (e.g., street_name, purchase_price) and the values of the those elements are of the right data type and within acceptable value ranges (e.g., purchase_price is a numerical value greater than zero and less than one thousand).