21

私が取り組んでいる残りのサービスの応答は、次の例に似ています。ここには 3 つのフィールドしか含まれていませんが、さらに多くのフィールドがあります。

{
    "results": [
        {
            "type": "Person",
            "name": "Mr Bean",
            "dateOfBirth": "14 Dec 1981"
        },
        {
            "type": "Company",
            "name": "Pi",
            "tradingName": "Pi Engineering Limited"
        }
    ]
}

上記 (draft-04) の JSON スキーマ ファイルを作成したいと思います。これは、次のことを明示的に指定します。

if type == Person then list of required properties is ["type", "name", "dateOfBirth", etc] 
OR
if type == "Company" then list of required properties is ["type", "name", "tradingName", etc]

ただし、それを行う方法のドキュメントや例を見つけることができません。

現在、私の JSON スキーマは次のようになっています。

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "type": "object",
    "required": ["results" ],
    "properties": {
        "results": {
            "type": "array",
            "items": {
                "type": "object",
                "required": ["type", "name"],
                "properties": {
                    "type": { "type": "string" },
                    "name": { "type": "string" },
                    "dateOfBirth": { "type": "string" },
                    "tradingName": { "type": "string" }
                }
            }
        }
    }
}

これを処理する方法のポインタ/例。

4

2 に答える 2

34

推奨されるアプローチは、 Json-Schema Web の Example2 に示されているものだと思います。「値で」スキーマを選択するには、列挙型を使用する必要があります。あなたの場合、それは次のようになります:

{
    "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" ] },
                . . . 
            }        
        }
    }
}
于 2013-08-22T15:01:52.900 に答える
10

ごめん、

意味がわかりません。質問は、最後の JSON スキーマ仕様の一部である「依存関係」キーワードに関するものですよね?

受け入れられた回答に「依存関係」が見つかりません(?)

最後のドラフトで簡単に説明されています。しかし、 http: //usingjsonschema.comでは、本でプロパティと定義の依存関係について説明しています。

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

29ページから開始(30ページで説明されているを参照)

"dependencies": {
     "shipTo":["shipAddress"],
     "loyaltyId":["loyaltyBonus"]
}
于 2014-12-20T00:04:59.837 に答える