RAML: Nested Schemas

KFunk picture KFunk · Nov 21, 2014 · Viewed 8.1k times · Source

1) When writing RAML, can I use nesting in my schema definition?

For example:

schemas:
  - DNSResponse: |
      {
        "type": "object",
        "properties": {
            "AnswerSection": {
                "type": "array",
                "items": (((I want a re-useable schema here. ex: ARecord)))
            },
            "AA": {"type": "boolean"},
            "AD": {"type": "boolean"},
            ...
        }
      }
  - ARecord: |
      {
        "type": "object",
        "properties": {
            "address": "string",
            "ttl": "number",
            "name": "string"
        }
      }

2) Can I use a choices/enum around a set of nestable schemas?

"items": [ARecord, MXRecord, PTRRecord, ...]

Answer

David Dossot picture David Dossot · Nov 21, 2014

1) Yes, you can. See this example. That would be:

"items": { "$ref": "ARecord" }

2) I believe this is possible in Draft 4 of JSON Schema, using the oneOf directive. I don't think this is supported by RAML though. Alternatively, you could create a base schema and have ARecord, MXRecord and PTRRecord extend this base schema and then allow items of the base schema. This won't be very semantically rich but could get you started.