3

スキーマを強制したいアイテムのリストを持つスキーマを構築しようとしています。

基本的に、スキーマに対して検証するデータは次のとおりです。

data = {
    "VIN" : "123",
    "timestamp" : "xxxx",
    "model" : "jeep",
    "inspections": [
        { "door_badge" : 
             {
                "expected": "yes",
                "image": "/image/path/here",
                "state": 1
            },
        },
        {
            "rear_badge" :
            {
                "expected" : "yes",
                "image" : "/image/path/here",
                "state": 1
            }


        }
    ],
}

そして、私のスキーママッピングはそのままですが、検証しようとするとエラーが発生するようです:

schema = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "inspection": {
            "type": "object",
            "properties": {
                "expected" : { "type": "string" },
                "found": { "type" : "string"},
                "state" : { "enum" : [0,1] },
                "image" : { "type" : "string"}
            },
            "required": ["state", "image"]
        },
        "inspections" : {
            "type" : "array",
            "items" : {
                "$ref" : "#/definitions/inspection"
            },
            "required" : ["items"]
        },

    },
    "type" : "object",
    "properties" : {
        "VIN" : { "type" : "string" },
        "timestamp" : { "type" : "string"},
        "model" : { "type" : "string"},
        "inspections" : { "$ref" : "#/definitions/inspections"}
    },
    "required": ["VIN", "timestamp", "model"]
}

基本的に、検査リスト内のサブリストが必要ですが、そのタイプのアイテムに基づいて検証することも必要です。


解決策: jruizaranguren の助けのおかげで、解決策は次のとおりです。

schema = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "inspection": {
            "type": "object",
            "properties": {
                "expected" : { "type": "string" },
                "found": { "type" : "string"},
                "state" : { "enum" : [0,1] },
                "image" : { "type" : "string"}
            },
            "required": ["state", "image", "expected"]
        },
    },
    "type" : "object",
    "properties" : {
        "VIN" : { "type" : "string" },
        "timestamp" : { "type" : "string"},
        "model" : { "type" : "string"},
        "inspections" : { 
            "type" : "array",
            "items" : {
                "type" : "object",
                "maxProperties": 1,
                "minProperties": 1,
                "additionalProperties" : {
                    "$ref" : "#/definitions/inspection"
                }
            }
        }
    },
    "required": ["VIN", "timestamp", "model", "inspections"]
}
4

1 に答える 1

3

問題は、配列内の各項目を次の形式に制限していることです。

{
    "expected": "yes",
    "image": "/image/path/here",
    "state": 1
}

ただし、オブジェクトの形式は次のとおりです。

{ "door_badge" : 
    {
        "expected": "yes",
        "image": "/image/path/here",
         "state": 1
    },
}

これを実現する 1 つの方法additionalPropertiesは、items句で使用することです。

"items" : 
    { 
            "type" : "object",
            "maxProperties": 1,
            "minProperties": 1,
            "additionalProperties" : {
                "$ref" : "#/definitions/inspection"
            }
    }

これらのプロパティ キーにいくつかのルールを適用できる場合 (たとえば、すべてが Badge で終わる必要patternPropertiesある) 、正規表現で句を使用できます。

于 2016-02-22T11:48:13.797 に答える