简体   繁体   中英

Json Schema - Using enumerations using reference

I'm trying to construct a JSON schema for my use case where I have the enumerations for a string in a separate file and want to refer that from my schema. How can I achieve that.

Sample Schema:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "card": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer"
                },
                "value": {
                    "type": "string",
                    "enum": {"$ref" : "reference to a file having list of enums"}
                  //I want to refer to a specific enum array (say value1's array)
                }
            }
        }
    },
    "required": [
        "card"
    ]
}

Enums file is like:

{
"value1": [..],
"value2": [..]
....
}

$ref should only be used to reference schemas. So, you could do something like this.

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "card": {
            "type": "object",
            "properties": {
                "id": { "type": "integer" },
                "value": { "$ref" : "/schemas/valueEnum.json" }
            }
        }
    },
    "required": ["card"]
}

/schemas/valueEnum.json

{ "enum": ["foo", "bar"] }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM