j2labs/dictshield — Issue #24: required=True/False is too binary ;-)
Author: alexanderdean
Created: 2011-10-29 00:04:27+00:00
State: open

A fairly common feature of RESTful APIs is that there are fields which are expected on Read (GET) which aren't settable on Create (POST or maybe PUT). Examples of these fields are auto-increment IDs and datestamps. To deal with this in Dictshield, I've had to create two documents for each RESTful resource:

class BaseRepresentation(Document):
id = URLField(required=True)
createdAt = DateTimeField(required=True)
updatedAt = DateTimeField(required=True)

class SalesShipment(BaseRepresentation):

class NewSalesShipment(Document):

Note that `NewSalesShipment` does not inherit from `BaseRepresentation` - because the `id`/`createdAt`/`updatedAt` fields shouldn't exist in a new sales shipment.

The above works fine but it is obviously less than DRY for long lists of fields. Also having a set of resources called `New*` looks a bit lame!

Just riffing here but maybe an alternative would be something like:

id = URLField(required=["GET","POST"])
merchant.validate("GET")

Just a thought! Feel free to close as not a bug, but thought it was worth raising.