首页 > 解决方案 > 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.

标签: restapi

解决方案


您不会在第二个端点上进行验证,因为您总是希望验证运行。如果您想在用户点击保存按钮之前将问题可视化,您应该进行客户端验证。然后它应该是在那个端点上发生的服务器端验证。如果验证失败,请注意您可以返回 http 状态 400,但如果需要,还可以返回一个 JSON 对象,其中包含有关任何补救建议的所有信息。我想,验证是一个实现设计主题,而不是 API 设计主题。


推荐阅读