kevinswiber/siren

Use object instead of array for actions

Opened this issue · 4 comments

Actions are contained within an array where each array element is an object that must contain a name field that is unique to the array. Client behavior is undefined if two actions have the same name.

This sounds exactly like a use case for using an object instead of an array, with the name field being the key for each action.

"actions": {
    "add-item": {
      "title": "Add Item",
      "method": "POST",
      "href": "http://api.x.io/orders/42/items",
      "type": "application/x-www-form-urlencoded",
      "fields": [
        { "name": "orderNumber", "type": "hidden", "value": "42" },
        { "name": "productCode", "type": "text" },
        { "name": "quantity", "type": "number" }
      ]
    }
  }

An object eliminates the possibility of undefined behavior.

Why not use an object?

m0x72 commented

The restriction for action field names to be unique, was introduced with the last merged pull request #64. It seems like an accident to me, as it forbids multiple values for an action field, as found in radio or checkbox inputs.
Let's wait for an answer on the PR. :)

@m0x72 I think we're talking about two difference things. I'm talking about the name field of an action object, not the name field of a field object. The word “field” is overloaded here. In one sense, it means a key-value pair in a JSON object. In the other sense, it means the siren concept of an input field.

The snippet I gave above is in contrast to the snippet in the README.md on the front page of this project. Below is the original snippet. Notice the snippet above replaces the array of action objects with an object of action objects, with each action object keyed using its name.

"actions": [
    {
      "name": "add-item",
      "title": "Add Item",
      "method": "POST",
      "href": "http://api.x.io/orders/42/items",
      "type": "application/x-www-form-urlencoded",
      "fields": [
        { "name": "orderNumber", "type": "hidden", "value": "42" },
        { "name": "productCode", "type": "text" },
        { "name": "quantity", "type": "number" }
      ]
    }
  ],
m0x72 commented

Ah, I'm so sorry. I mixed them up.

So yes, indeed a valid concern. It simplifies action lookup as well, but likely breaks any current implementations. Calls for a new major version?

pke commented

I also wondered why it is not an object?