In JSON schema, define and reference a reusable enum type?

Viewed 1068

I noticed the following: Reusable enum types in json schema , which talks about defining a reusable enum type in JSON schema.

I would have assumed USING this reusable enum type would be trivial, simply specifying (in this case) the value of "MyEnum" for a "type" value.

I don't know if the results from Oxygen XML are authoritative, but I tried something like the following:

{
    "$schema": "https://json-schema.org/draft/2019-09/schema#",
    "type": "object",
    "properties": {
        "content": {"$ref": "#/definitions/content_type"}
    },
    "additionalProperties": false,
    "definitions": {
        "costCategory_type": {
            "type": "object",
            "enum": ["VH", "H", "M", "L"]  
        },
        "allowedDevices_type": {
            "type": "object",
            "properties": {
                "costCategory": {
                    "type": "costCategory_type"
                },

On the line near the bottom of this, where I reference "costCategory_type", Oxygen gives me a syntax error, saying

#/definitions/allowedDevices_type/properties/costCategory/type: unknown type: [costCategory_type]

What am I missing?

1 Answers

Yes, the type keyword can only have values from the list null, boolean, object, array, string, number, integer. You can reference definitions with the $ref keyword:

...
"properties": {
  "costCategory": {
    "$ref": "#/definitions/costCategory_type",
  }
}

(incidentally, your definition won't ever evaluate successfully as-is since you define it as being the "object" type, but the list of values in the enum are all strings.)

Related