interagent / prmd

JSON Schema tools and doc generation for HTTP APIs
MIT License
2.1k stars 172 forks source link

Are you planning to support oneOf, anyOf, etc for attributes in prmd doc util? #193

Open mainameiz opened 9 years ago

mainameiz commented 9 years ago

Hello!

Currently it's assumed that attribute can have only one type and this case doesn't work.

Example of my schema.

{
  "id": "/some_controller.rb",
  "properties": {
    "some_timestamp": {
      "oneOf": [
        { "$ref": "/_datetime" },
        { "type": "null" }
      ]
    }
  },
  "links": []
}
{
  "id": "/_datetime",
  "type": "string",
  "description": "datetime in rfc3339 format with 3 fractional seconds",
  "example": "2012-01-01T12:00:00.123Z",
  "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-2][0-9]:[0-5][0-9]:[0-5][0-9](\\.[0-9]{3})?(Z|[\\-+][0-9]{2}:[0-5][0-9])$"
}

It is a valid json-schema and allows to DRY schema definitions.

In addition it may be applicable in situations when for example you want to support different time formats.

{
  "id": "/complex_datetime",
  "oneOf": [
    { "$ref": "/_datetime_iso8601" },
    { "$ref": "/_datetime_rfc3339" },
    { ... }
  ]
}
geemus commented 9 years ago

For the first example I think you can simplify by having datetime use a compound type, ie "type": ["null", "string"]. And/or use a ref and then give it that type as a peer (which should override the more generic type inside of datetime).

I think it might be good to support the other case too, but I haven't run into cases where it was actually needed quite yet. So while I'm generally in favor of the idea, I think I would just as soon wait for a concrete need.

Thanks!