How to expose a validation API in a RESTful way?

Viewed 51697

I'm generally a fan of RESTful API design, but I'm unsure of how to apply REST principles for a validation API.

Suppose we have an API for querying and updating a user's profile info (name, email, username, password). We've deemed that a useful piece of functionality to expose would be validation, e.g. query whether a given username is valid and available.

What are the resource(s) in this case? What HTTP status codes and/or headers should be used?

As a start, I have GET /profile/validate which takes query string params and returns 204 or 400 if valid or invalid. But validate is clearly a verb and not a noun.

5 Answers

A very common scenario is having a user or profile signup form with a username and email that should be unique. An error message would be displayed usually on blur of the textbox to let the user know that the username already exists or the email they entered is already associated with another account. There's a lot of options mentioned in other answers, but I don't like the idea of needing to look for 404s meaning the username doesn't exist, therefore it's valid, waiting for submit to validate the entire object, and returning metadata for validation doesn't help with checking for uniqueness.

Imo, there should be a GET route that returns true or false per field that needs validated.

/users/validation/username/{username}

and

/users/validation/email/{email}

You can add any other routes with this pattern for any other fields that need server side validation. Of course, you would still want to validate the whole object in your POST.

This pattern also allows for validation when updating a user. If the user focused on the email textbox, then clicked out for the blur validation to fire, slightly different validation would be necessary as it's ok if the email already exists as long as it's associated with the current user. You can utilize these GET routes that also return true or false.

/users/{userId:guid}/validation/username/{username}

and

/users/{userId:guid}/validation/email/{email}

Again, the entire object would need validated in your PUT.

Related