rest - API Design for Validaion and Save scenarios
问题描述
I have an API where we want to save commercial breaks. There are also validations in terms of rules such as hour limit not breach, average duration with in limits etc.
Should such validations have a different endpoint such as POST commercialbreaks/validation and then we validate the same from UI
a. UI ->commercialbreaks/validation , if pass then UI hit commercialbreaks->POST /commercialbreaks.
b. or should we have a single endpoint i.e POST /commercialbreaks and then have /commercialbreaks/validation be called from inside inside this API.
Validation takes user input too for rules to validate against. We will have to pass the same whenever we hit POST /commercialbreaks if we go for b).
THere is a also a possibility that post validation there is a delay in save and then by then some changes have happened in system so we may need revalidation again.
Which is a better design approach. Also, we have a situation where Validation gives a message that it failed/passed and if user has the Override prvilige then he will be be able to override the validation and still be able to save the breaks. In this case should we have use the same endpoint POST /commercialbreaks with override flag =true/false. Please suggest.
解决方案
您不会在第二个端点上进行验证,因为您总是希望验证运行。如果您想在用户点击保存按钮之前将问题可视化,您应该进行客户端验证。然后它应该是在那个端点上发生的服务器端验证。如果验证失败,请注意您可以返回 http 状态 400,但如果需要,还可以返回一个 JSON 对象,其中包含有关任何补救建议的所有信息。我想,验证是一个实现设计主题,而不是 API 设计主题。
推荐阅读
- blazor - 在服务器端 Blazor 中,如何取消页面或组件长时间运行的后台任务?
- javascript - React.js 显示列表的详细信息
- javascript - Google Maps wont work under Mobile Mode of Google Chrome
- laravel - 当我尝试将 Bootstrap 和 Fontawesome 包含到 Laravel 项目中时出错
- html - 如何在整个部分制作 youtube 视频背景封面
- sql - 编写 Rails 迁移以将布尔值折叠成枚举
- javascript - 从本地 .js 读取本地 .json 而无需 repos
- php - Laravel 中的路由不起作用(显示空白页)
- c++ - Unicode 字符正在打印,但不如预期
- laravel - 错误:创建表单操作字段的未定义路由 - laravel 5.8