2

I'm developing a web application using a custom REST service. For user registration I need the user to provide a date of birth. There is a min and max value for the date of birth, which is known by the API.

The form's date of birth consists of 3 selectboxes, day, month, year, and has front-end validation in place, which will validate the date entered (and make sure the date exists, etc.).

I do not want to hard code the business rules for min/max age in the front end, as this would mean it's maintained on two separate locations. However, I have nothing in the API right now that provides this information (as a resource or so), and I'm also not sure if this is a logical thing to add for an API.

How should I retrieve this business rule information properly from a REST API?

Edit: Also, as I will need to generate the values within the year selectbox, I need to know the valid years. This makes it much like any value set (i.e. available user titles (Mr, Mrs) which in fact are resources that I get from the API). With that in mind, it would sound like a date-of-birth value set, which could theoretically be a list of all valid days (which would be insane), or the range that is valid.

KrekkieD
  • 967
  • 9
  • 23
  • have you looked into HATEOAS? (Hypermedia as the Engine of Application State) https://en.wikipedia.org/wiki/HATEOAS – Tim Ogilvy Mar 06 '17 at 00:11

2 Answers2

1

At my company we have a path called /environment/. Perhaps this could be a place to put it? Or /user/environment/?

Of course this is your own decision. I haven't seen any practices on it and therefore I think the solution will not affect the RESTfulness of your API that much.

Piddien
  • 1,436
  • 4
  • 18
  • 38
1

I see this a question of wether you wan't to pollute your API with business-rule resources. Actually I see it as belonging to the domain of documentation since it's some sort of metainformation.

If I want to build a client that is using your API it should be up to me whether I wan't to validate the input in the client or not.

However It would be very nice to get information back if I try to register with invalid input what it is that is wrong.

But to answer your question, either you do as @Piddien suggest, alternatively I think that WADL might have some support for supplying valid inputs. However as far as I know it isn't widely used (I might be very wrong here).

Daniel Figueroa
  • 10,348
  • 5
  • 44
  • 66
  • Oh and WADL seems to be quite controversial take a look here http://bitworking.org/news/193/Do-we-need-WADL and this thread might be of value http://stackoverflow.com/questions/4111646/restful-services-wsdl-equivalent – Daniel Figueroa Jul 16 '14 at 15:29
  • Part of documentation, I guess that is true. Expecting all implementors of the service to watch the documentation (and changes made per version) is manual labor, therefore (more) error prone and not my preference. I have also changed the question to indicate that I need to populate the year select box with valid years, which would make it more of a resource. – KrekkieD Jul 16 '14 at 16:16
  • Then again, I'm deciding to put it in select boxes, whereas the value itself is dd-mm-yyyy.. – KrekkieD Jul 16 '14 at 16:26
  • It's very true that you shouldn't expect that users/clients have read the documentation, actually you shouldn't even trust clients -> that is you should `always` validate input on the server. But still, using your API requires reading some documentation. If you look at for example imgur or twitter they don't provide an API for looking up what is valid input. That is checked anyway when an user issues a request. – Daniel Figueroa Jul 16 '14 at 17:05
  • How do you feel about JSON Schema as a resource to define object/action specifications? http://json-schema.org/example1.html – KrekkieD Jul 23 '14 at 12:25
  • I like JSON Schemas but they are not very standardized yet, I think version 4 is pretty good though. They do quite easily become quite complex. But I guess that is okay for a schema. Also I'm not quite sure that you can control for example a daterange unless you supply a list of valid values. Which might be okay in your case. – Daniel Figueroa Jul 24 '14 at 07:19