知识卡片
发送前校验请求正文
客户端提交不合法的请求正文会增加服务拒绝与返工,应在发送前检查。
稳健性原则在发送侧要求尽量保守,主动保证自己发出的消息有效。客户端发出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).