【问题标题】:Unable to validate the 'required' properties in an array of a JSON无法验证 JSON 数组中的“必需”属性
【发布时间】:2020-07-27 08:52:46
【问题描述】:

我在这样的文件中有一个draft-7JSON 架构:

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "type": "object",
    "required": [
        "tenantid",
        "owningObjectId",
        "owningObject",
        "cudAction",
        "message"
    ],
    "properties": {
        "tenantid": {
            "type": "string",
            "default": ""
        },
        "owningObjectId": {
            "type": "string",
            "default": ""
        },
        "owningObject": {
            "type": "string",
            "default": "",
            "pattern": "^TeamUser$"
        },
        "cudAction": {
            "type": "string",
            "default": "",
            "pattern": "^c$"
        },
        "messageDateTime": {
            "type": "string",
            "default": ""
        },
        "encrypted": {
            "type": "boolean",
            "default": false
        },
        "message": {
            "type": "array",
            "items": {
                "type": "object",
                "required": [
                    "name",
                    "teamcode",
                    "enabled"
                ],
                "properties": {
                    "name": {
                        "type": "string",
                        "default": ""
                    },
                    "teamcode": {
                        "type": "string",
                        "default": ""
                    },
                    "org": {
                        "type": "string",
                        "default": ""
                    },
                    "enabled": {
                        "type": "boolean",
                        "default": false,
                    },
                    "version": {
                        "type": "string",
                        "default": ""
                    },
                    "orgDisplay": {
                        "type": "string",
                        "default": ""
                    }
                }
            }
        }
    }
}

我正在使用以下架构验证 JSON/响应:

# pip install jsonschema
from jsonschema import Draft7validator
def validate_response(response, schema_file_path: str) -> bool:
    """
    Validate the input message based on the input schema provided.
    :reference http://json-schema.org/understanding-json-schema/
    :param response: response received as JSON
    :param schema_file_path: The schema file path
    :return validated: returns True if valid response else False
    """
    validated = True
    with open(schema_file_path, "r") as schema_reader:
        schema = json.loads(schema_reader.read())
    errors = Draft7Validator(schema).iter_errors(response)
    for error in errors:
        print(error.message)
        validated = False
    if validated:
        print(f"Valid response")
    return validated

但是,对于像下面的 faulty_json_response 这样的 JSON,其中 message 字段值数组为空,并且 message 字段下不存在任何 required 属性,验证器不会抛出任何错误。可能是什么原因?

faulty_json_response = {
        "tenantid": "5e3bb57222b49800016b666f",
        "owningObjectId": "5e680018ceb7d600012e4375",
        "owningObject": "TeamUser",
        "cudAction": "c",
        "messageDateTime": "1584460716.01416",
        "encrypted": false,
        "message": [],
    }

如果需要更多详细信息,请告诉我。谢谢。

【问题讨论】:

    标签: python json jsonschema json-schema-validator python-jsonschema


    【解决方案1】:

    items 关键字将子模式值(包括我们所需的)应用于适用数组中的每个项目(在您的情况下为 message)。

    假设您在 message 数组中没有任何项目,则不会应用子模式,因此您不会收到任何验证错误。

    如果您想指定数组的最小项目数,您可以使用 minItems 关键字...

    此关键字的值必须是非负整数。

    如果数组实例的大小为 大于或等于此关键字的值。

    省略此关键字与值为 0 的行为相同。

    https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.4.4

    您可以在此现场演示中看到这一点 https://jsonschema.dev/s/yMM0c

    【讨论】:

    • 太好了,本。将此minItems 与现有的required 字段相结合使我能够实现所需的行为。非常感谢。 :)
    • 您介意分享一下您是如何在架构中包含 minItems 的吗?
    • 它需要被添加到message 的值模式中,作为typeitems 的兄弟。如果这还不够,请告诉我,我会做一个 GitHub Gist。
    猜你喜欢
    • 2020-02-05
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多