34

JSON 項目の順序付けられていない配列があります。仕様https://datatracker.ietf.org/doc/html/draft-zyp-json-schema-03#section-5.5によると、以下の json スキーマは、配列内のオブジェクトがその順序で表示される場合にのみ検証されます。オブジェクトの順序や数に関係なく、配列内のオブジェクトを検証するだけです。仕様から、これがどのように行われるのか理解できないようです。

"transactions" : {
    "type" : "array",
    "items" : [
        {
            "type" : "object",
            "properties" : {
                "type" : {
                    "type" : "string",
                    "enum" : ["BUILD", "REASSIGN"]
                }
            }
        },
        {
            "type" : "object",
            "properties" : {
                "type" : {
                    "type" : "string",
                    "enum" : ["BREAK"]
                }
            }
        }
    ]
}
4

5 に答える 5

61

JSON スキーマの Google グループで同じ質問をしたところ、すぐに回答がありました。ユーザー fge は、彼の回答をここに投稿するように依頼しました。

こんにちは、

現在の仕様はドラフト v4 であり、ドラフト v3 ではありません。より具体的には、検証仕様は次のとおりです。

https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00

Web サイトが最新ではありません。理由がわかりません... プル リクエストを送信します。

ドラフト v4 では、これを使用できます。

{
    "type": "array",
    "items": {
        "oneOf": [
            {"first": [ "schema", "here" ] }, 
            {"other": [ "schema": "here" ] }
        ]
    }  
}

たとえば、これは項目が文字列または整数のいずれかである配列のスキーマです (ただし、より単純な方法で記述できます)。

{
    "type": "array",
    "items": {
        "oneOf": [
            {"type": "string"},
            {"type": "integer"}
        ]
    }
}

これが正解です。修正されたスキーマには、次のものが含まれるようになりました。

"transactions" : {
    "type" : "array",
    "items" : {
        "oneOf" : [
            {
                "type" : "object",
                "properties" : {
                    "type" : {
                        "type" : "string",
                        "enum" : ["BUILD", "REASSIGN"]
                    }
                }
            },
            {
               "type" : "object",
               "properties" : {
                 "type" : {
                   "type" : "string",
                   "enum" : ["BREAK"]
                  }
               }
            }
        ]
    }
}
于 2013-03-28T02:38:29.903 に答える
4

私もかなり長い間これを調べてきました。しかし、実用的な解決策を見つけることができませんでした。たとえば、スキーマが1つしかない場合は正常に機能します。

"transactions" : {
          "type" : "array",
          "items" : 
          {
            "type" : "object",
            "properties" : {
              "type" : {
                "type" : "string",
                "enum" : ["BREAK"]
              },
          }
}

次に、配列ブラケットをスキップして、オブジェクトを使用します。しかし、やりたいことがあるなら、確実な答えはないようです。これは私がこれまでに見つけた唯一のものです: http://the-long-dark-tech-time.blogspot.se/2012/12/using-json-schema-with-array-of-mixed.html

于 2013-03-27T21:24:15.387 に答える
1

私の場合、配列の最初の要素には特定の形式が必要で、残りの要素には別の形式があります。これが私の解決策です:

my_schema = {
    "type": "object",
    "properties": {
        "token": {"type": "string"},
        "service_id": {"type": "string"},
        "promo_code": {"type": "string"},
        "path": {
            "type": "array",
            "items": [
                {
                    "type": "object",
                    "properties": {
                        "address": {"type": "string"},
                        "lat": {"type": "number"},
                        "lng": {"type": "number"}
                    },
                    "required": ["address", "lat", "lng"]
                },
                {
                    "type": "object",
                    "properties": {
                        "address": {"type": "string"},
                        "lat": {"type": "number"},
                        "lng": {"type": "number"},
                        "district_id": {"type": "number"},
                        "ward_code": {"type": "number"},
                        "weight": {"type": "number"}
                    },
                    "required": ["address","lat", "lng","ward_code", 
                                 "district_id", "weight"]
                }
            ]
        }
    },
    "required": ["token", "service_id", "path"]
}

上記のスキーマは、パスの 2 番目の要素から、district_id、ward_code、weight が必要であることを意味します。

于 2019-12-03T08:12:41.187 に答える