How to use definitions in JSON schema (draft-04)

后端 未结 2 844
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 08:01

The rest service response I am working with is similar to following example, I have only included 3 fields here but there are many more:

{
    \"results\": [         


        
相关标签:
2条回答
  • 2020-11-28 08:02

    I think the recommended approach is the one shown in Json-Schema web, Example2. You need to use an enum to select schemas "by value". In your case it would be something like:

    {
        "type": "object",
        "required": [ "results" ],
        "properties": {
            "results": {
                "type": "array",
                "items": {
                    "oneOf": [
                        { "$ref": "#/definitions/person" },
                        { "$ref": "#/definitions/company" }
                    ]
                }
            }
        },
        "definitions": {
            "person": {
                "properties": {
                    "type": { "enum": [ "person" ] },
                    "name": {"type": "string" },
                    "dateOfBirth": {"type":"string"}
                },
                "required": [ "type", "name", "dateOfBirth" ],
                "additionalProperties": false
            },
            "company": {
                "properties": {
                    "type": { "enum": [ "company" ] },
                    . . . 
                }        
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 08:29

    Sorry,

    I don't get the point. The question is about the 'dependencies' keyword which is part of the last JSON Schema specification, right?

    I do not find 'dependencies' in the accepted answer (?)

    It is briefly explained in the last draft. But http://usingjsonschema.com explained both property and definition dependencies in the book:

    http://usingjsonschema.com/assets/UsingJsonSchema_20140814.pdf

    start at page 29 (see, explained at page 30)

    "dependencies": {
         "shipTo":["shipAddress"],
         "loyaltyId":["loyaltyBonus"]
    }
    
    0 讨论(0)
提交回复
热议问题