Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
985 views
in Technique[技术] by (71.8m points)

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

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?

question from:https://stackoverflow.com/questions/65889922/in-json-schema-define-and-reference-a-reusable-enum-type

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...