JSON模式-条件验证

时间:2019-06-03 09:17:12

标签: jsonschema json-schema-validator ajv

我有以下架构。我已经尽力实现了它,但是它仍然没有按照我想要的那样工作。

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "title": "Ordering pizza",
    "propertyNames": {
        "enum": [
            "q-who-did-you-order-from",
            "q-did-they-accept-your-order",
            "q-how-much-was-the-bill",
            "q-why-didnt-they-accept"
        ]
    },
    "properties": {
        "q-who-did-you-order-from": {
            "type": "string",
            "title": "Who have you ordered pizza from?",
            "maxLength": 50,
            "errorMessages": {
                "required": "Enter who you ordered from",
                "maxLength":
                    "Who you ordered from must be 50 characters or less"
            }
        },
        "q-did-they-accept-your-order": {
            "title": "Have they accepted your order?",
            "type": "boolean",
            "errorMessages": {
                "required":
                    "Select yes if they have accepted your order"
            }
        },
        "q-how-much-was-the-bill": {
            "type": "string",
            "title": "How much was the bill?",
            "maxLength": 50,
            "errorMessages": {
                "required": "Enter an amount",
                "maxLength": "Amount must be 50 characters or less"
            }
        },
        "q-why-didnt-they-accept": {
            "type": "string",
            "title": "Why wasnt your order accepted?",
            "description":
                "If you do not know you can say so.",
            "maxLength": 50,
            "errorMessages": {
                "required": "Enter a reason",
                "maxLength": "Reason must be 50 characters or less"
            }
        }
    },
    "required": ["q-who-did-you-order-from", "q-did-they-accept-your-order"],
    "allOf": [
        {
            "$ref": "#/definitions/if-false-then-q-why-didnt-they-accept-is-required"
        },
        {
            "$ref": "#/definitions/if-true-then-q-how-much-was-the-bill-is-required"
        }
    ],
    "definitions": {
        "if-false-then-q-why-didnt-they-accept-is-required": {
            "if": {
                "properties": {
                    "q-did-they-accept-your-order": {
                        "const": false
                    }
                }
            },
            "then": {
                "required": ["q-why-didnt-they-accept"],
                "propertyNames": {
                    "enum": [
                        "q-who-did-you-order-from",
                        "q-did-they-accept-your-order",
                        "q-why-didnt-they-accept"
                    ]
                }
            }
        },
        "if-true-then-q-how-much-was-the-bill-is-required": {
            "if": {
                "properties": {
                    "q-did-they-accept-your-order": {
                        "const": true
                    }
                }
            },
            "then": {
                "required": ["q-how-much-was-the-bill"],
                "propertyNames": {
                    "enum": [
                        "q-who-did-you-order-from",
                        "q-did-they-accept-your-order",
                        "q-how-much-was-the-bill"
                    ]
                }
            }
        }
    }
}

期望的是,用户将输入“您订购的q”和“您接受订购的q”的值,然后根据他们的答案仅输入其余两个问题之一对于q,他们接受您的订单了。

因此,以下输入应有效:

{
    "q-did-you-order-from": "Pizza hut",
    "q-did-they-accept-your-order": "true",
    "q-how-much-was-the-bill": "20"
}



{
    "q-did-you-order-from": "Pizza hut",
    "q-did-they-accept-your-order": "false",
    "q-why-didn't-they-accept": "Incorrect card details"
}

类似地,我希望以下输入无法通过验证,并为包含空白字符串的字段抛出“必需”错误。第一个应该抛出错误,因为q-为什么不接受它们为空:

{
    "q-did-you-order-from": "Pizza hut",
    "q-did-they-accept-your-order": "false",
    "q-why-didn't-they-accept": ""
}

这个应该抛出一个错误,因为q-多少钱是空的。

{
    "q-did-you-order-from": "Pizza hut",
    "q-did-they-accept-your-order": "true",
    "q-how-much-was-the-bill": ""
}

确实如此!这按预期工作。但是,我们发现了一个错误,该错误是由于用户没有输入q-是否接受他们的订单的答案而引起的。这些问题的答案在表单提交时通过浏览器发布。在浏览器中,布尔值问题显示为是/否单选按钮。结果,当用户不检查任何一个收音机,而是尝试提交表单时,将完全省略收音机的答案。发送的数据对象如下所示:

{
    "q-did-you-order-from": "Pizza hut",
    "q-how-much-was-the-bill": "",
    "q-why-didn't-they-accept": "",
}

我在这里的预期结果:

AJV仅对q接受您的订单抛出一个“必需”错误。它不应该对其他任何东西抛出“必需”错误,因为不需要q多少票据和为什么不接受q,除非q的相关值是-他们接受您的订单。

我的实际结果:

AJV对所有三个空输入都引发错误。

所以我的问题是,如何使AJV验证此模式,并且在问题没有得到回答时,仅q-did-他们接受您的订单就引发所需的错误。

编辑:

AJV的输出如下:

[
    {
        "keyword": "required",
        "dataPath": "",
        "schemaPath": "#/required",
        "params": {
            "missingProperty": "q-did-they-accept-your-order"
        },
        "message": "should have required property 'q-did-they-accept-your-order'"
    },
    {
        "keyword": "required",
        "dataPath": "",
        "schemaPath": "#/definitions/if-false-then-q-why-didnt-they-accept-is-required",
        "params": {
            "missingProperty": "q-why-didnt-they-accept"
        },
        "message": "should have required property 'q-why-didnt-they-accept'"
    },
    {
        "keyword": "if",
        "dataPath": "",
        "schemaPath": "#/definitions/if-false-then-q-why-didnt-they-accept-is-required/if",
        "params": {
            "failingKeyword": "then"
        },
        "message": "should match \"then\" schema"
    },
    {
        "keyword": "required",
        "dataPath": "",
        "schemaPath": "#/definitions/if-true-then-q-how-much-was-the-bill-is-required/then/required",
        "params": {
            "missingProperty": "q-how-much-was-the-bill"
        },
        "message": "should have required property 'q-how-much-was-the-bill'"
    },
    {
        "keyword": "if",
        "dataPath": "",
        "schemaPath": "#/definitions/if-true-then-q-how-much-was-the-bill-is-required/if",
        "params": {
            "failingKeyword": "then"
        },
        "message": "should match \"then\" schema"
    }
]

1 个答案:

答案 0 :(得分:1)

您的if / then模式应用程序缺少一部分。让我们以这个简单的模式为例。

{
  "if": {
    "properties": {
      "foo": { "const": true }
    }
  },
  "then": {
    "required": ["bar"]
  }
}

如果我根据此架构验证{},则将失败,要求提供属性“ bar”。由于/if模式不需要“ foo”属性,因此{}是有效的,因此/then模式适用。要解决此问题,您只需要使“ foo”属性成为必需。

{
  "if": {
    "properties": {
      "foo": { "const": true }
    },
    "required": ["foo"]
  },
  "then": {
    "required": ["bar"]
  }
}

现在,{}对模式有效。 /then模式仅在存在“ foo”属性且其值为true时适用。