This may be easiest to explain with an example use case.
Let's say I have an e-commerce site where users can add items to their shopping cart. When adding items to the cart, users can type in the quantity that they want to add, and then click an
Add button. Clicking
Add will send a request to the server. The server will first validate that the current inventory has enough of the item to meet the requested quantity, if there are enough it adds the item to the cart and returns a successful response, otherwise it returns an error response.
To enhance the user experience, it would be nice to validate the quantity the user entered before they even click
Add. This would require sending a request to the server which will check if clicking
Add would result in an error, but it will not commit any changes to the cart if there is no error.
A coworker of mine suggested adding a query parameter
?intent=validate to any endpoints that require this kind of functionality. This sounds like a good idea because I will not have to create extra endpoints.
Are there any common conventions for REST APIs to handle this kind of "validate but don't commit anything" request? Does the
?intent=validate approach raise any red flags?
Thanks for the feedback, but I should probably clarify some things.
- I am not really working on an e-commerce site, I just used that for an easy to explain example. Really I am working with a document management platform.
- Users may be requesting bulk actions like moving 100 documents and folders to a folder. There are many things that need to be validated for any such request, like the user's rights over the documents and folders, name collisions, not moving a folder into itself, etc. So I really want to validate every time a user checks a checkbox in a list of documents to move.
- The API will still validate when a user clicks
Submitto commit their changes, because there are other users performing actions at the same time. The point is to give users early feedback, so they don't spend time checking 100 boxes only to find out that they need to rename 2 of the documents before committing.
- There are many other bulk requests in this system that need to be treated the same way, such as modifying user rights over objects, bulk deletes, assigning multiple users to user groups, etc. So it would be nice if each type of request did not require separate validate and commit endpoints.