我应该如何设计 RESTful URL 来验证对象 [英] How should I design a RESTful URL to validate an object

查看:48
本文介绍了我应该如何设计 RESTful URL 来验证对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不脱离 RESTful 范式的情况下,您如何以 RESTful 方式对对象验证进行建模?最好解释一下我提出的理论用例...

Without moving away from the RESTful paradigm, how could you model object validation in a RESTful way? Best to explain the theoretical use case I've come up with...

假设您有一个系统,它有一个非常薄的 Web 层,可以调用后端 RESTful 服务.假设用户访问了一个注册表单并提交了它,Web 层会将未经验证的数据直接发送到后端服务,如果服务以 JSON 格式响应验证错误,这些可以作为 HTML 发送回用户.

Imagine you have a system with a very thin web layer making calls to back-end RESTful services. Say a user visited a registration form and submitted it, the web layer would send the unvalidated data straight to a back-end service and, if the service responds with validation errors in JSON format, these can be sent back to the user as HTML.

但是,假设我们希望在表单上具有 AJAX 行为.例如,用户输入他们的电子邮件地址,我们想使用 AJAX 进行验证,如果他们的电子邮件地址已经注册,则向用户发送错误.

However, imagine we want to have AJAX behaviour on the form. For example, the user enters their email address and we want to validate using AJAX, sending an error to the user if their email address is already registered.

实现单个调用来仅验证电子邮件地址是否有意义,还是可以在后端服务中发送和验证整个对象?如果是后者,您可以使用什么 URL 来仅验证一个对象,而不是实际创建它?

Would it make sense to implement a single call to validate just the email address, or could the whole object be sent and validated in a back-end service? If the latter, what URL could you use to only validate an object, rather than actually create it?

推荐答案

过去我使用沙盒子资源的概念来执行您的建议,

In the past I have used the notion of a sandbox sub-resource to do what you are suggesting,

http://example.com/customer/23/sandbox

这允许我发布增量并应用和验证更改但实际上并未提交.这对于传统的保存/取消"类型对话框非常有效.

This allows me to POST deltas and have the changes applied and validated but not actually committed. This works quite well for the traditional "save/cancel" type dialogs.

然而,我发现处理这些增量真的很痛苦,所以我开发了一种不同的媒体类型,在客户端记录一系列事件,然后将该文档发布到沙箱资源.通过重播事件序列,我可以以更简单的方式更新和验证服务器端资源.

However, I found dealing with those deltas to be a real pain, so I developed a different media type that recorded a sequence of events on the client and then posted that document to the sandbox resource. By replaying the sequence of events I could update and validate the server side resource in a simpler fashion.

后来我意识到我真的不需要独特的沙箱"资源,现在我只是将事件序列"文档直接发布到它所影响的资源.我在文档本身中有一些数据,用于确定更改是永久的还是暂时的.这仅取决于用户是否按下了保存按钮.

Later on I realized that I really didn't need the distinct "sandbox" resource and now I just post the "sequence of events" document directly to the resource it is affecting. I have some data in the document itself that determines whether the changes are going to be permanent or just transient. It just depends if the user has pressed the save button yet or not.

这篇关于我应该如何设计 RESTful URL 来验证对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆