As a part of capabilities discovery for a resource provided by a RESTful API, I am looking for a way for the service to announce accepted values for an attribute. Consider the following example, where an apple
resource has an attribute color
:
GET /apples/17
This request yields:
{
"name": "My yummy apple",
"color": "green"
}
For a client to understand what color
values are valid when for instance PUT
ting a new version of this apple, I can think of many possible ways. However I haven't found any best practices here. The HTTP OPTIONS verb seems not to be made for this fine-grained kind of discovery. Should I just add an array attribute to the /apples
collection:
GET /apples
Response:
{
...
"colorValues": ["red", "green"]
}
Are there any better and more commonly used ways?
EDIT:
Just realized that one possible way would be to add a resources for schemas for all "real" resources. Something liked GET /schemas/apple
that would yield a JSON Schema representation for the apple
resource. Modified example from json-schema.org:
{
"id": "http://foo.bar/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "schema for an apple resource",
"type": "object",
...
"colorValues": {
"enum": [ "red", "green" ]
}
}
I have not found any examples of this though.