apiaryio / api-elements

API Elements is a structure for describing APIs and the complex data structures used within them.
http://apielements.org/
MIT License
28 stars 10 forks source link

Move enumerations to enumerations attribute in enum #28

Closed kylef closed 7 years ago

kylef commented 7 years ago

Enum was designed a little different to other data structure elements. Other Data Structure elements contain a value of the data structure under content, however enum doesn't contain a single value that the enum was representing but instead all the possible values (enumerations) that may be inside the enum.

This pull request changes enum so that the enum content contains the single selected enumeration, and move the available enumerations to the enumerations attribute. This allows the element to follow the normal data structure rules regarding the type for default and samples where they would match the same type as the data structure elements content.

Examples

To describe an enum with 4 possible string values, north, east, south, west we could represent that as an enum element as follows:

{
  "element": "enum",
  "attributes": {
    "enumerations": {
      "element": "array",
      "content": [
        {
          "element": "string",
          "content": "north"
        },
        {
          "element": "string",
          "content": "east"
        },
        {
          "element": "string",
          "content": "south"
        },
        {
          "element": "string",
          "content": "west"
        }
      ]
    }
  }
}

To describe the same enum with the four directions, and to provide a default value of north. We can represent that as an enum as follows:

{
  "element": "enum",
  "attributes": {
    "default": {
      "element": "string",
      "content": "north"
    },
    "enumerations": {
      "element": "array",
      "content": [
        {
          "element": "string",
          "content": "north"
        },
        {
          "element": "string",
          "content": "east"
        },
        {
          "element": "string",
          "content": "south"
        },
        {
          "element": "string",
          "content": "west"
        }
      ]
    }
  }
}

To describe the same enum, but also to provide a selected sample value of south:

{
  "element": "enum",
  "attributes": {
    "default": {
      "element": "string",
      "content": "north"
    },
    "enumerations": {
      "element": "array",
      "content": [
        {
          "element": "string",
          "content": "north"
        },
        {
          "element": "string",
          "content": "east"
        },
        {
          "element": "string",
          "content": "south"
        },
        {
          "element": "string",
          "content": "west"
        }
      ]
    }
  },
  "content": {
      "element": "string",
      "content": "south"
    }
}
pksunkara commented 7 years ago

Can you add an example for non-selected sample in PR description? I am approving it for now, but not merging yet since @klokane and @w-vi need to take a look too.

klokane commented 7 years ago

:+1:

klokane commented 7 years ago

Ops, we need change description.

Enumeration type. Exclusive list of possible elements. The elements in content's array MUST be interpreted as mutually exclusive.