json模式验证两个不同的 json 响应

jsonschema validating two different json responses

我有两个不同的 JSON 响应,每个响应都有不同的字段名称,但是都使用 jsonschema 库使用定义的模式成功验证。

这是定义的架构

query_schema = {
    "type": "object",
    "properties" : {
        "pltfrm_nm": {"type" : "string"},
        "srvr_nm": {"type": "string"},
        "db_nm": {"type": "string"},
        "tbl_nm": {"type": "string"},
        "ip_addr_id": {"type": "string"},
        "usr_id": {"type": "string"},
        "sql_txt": {"type": "string"},
        "timestmp": {"type": "string"},
    },
}

这里有两种不同的回应:

input = {'pltfrm_nm': 'p1', 'srvr_nm': 'server', 'db_nm': 'some db', 'tbl_nm': 'some table',
         'ip_addr_id': '999.999.9999', 'usr_id': 'my id', 'sql_txt': "sql text here", 'timestmp': 'aaaa)'}
validate(instance=input, schema=query_schema)

input = {'why': 'p1', 'does': 'server', 'this': 'some db', 'still': 'some table',
         'validate': '999.999.9999', 'i': 'my id', 'do': "sql text here", 'not': 'aaaa',
         'understand': 'hello'}
validate(instance=input, schema=query_schema)

在第二个输入中,我将所有字段命名为不同的名称并添加了一个新字段 understand。这些都不会抛出 ValidationError。这是图书馆:https://pypi.org/project/jsonschema/。为什么第二个不报错?

JSON 架构是基于约束而不是基于权限的。 一个空的 JSON 模式 {} 意味着任何东西都是有效的。

让我们看看关键字properties添加了什么约束...

Validation succeeds if, for each name that appears in both the
instance and as a name within this keyword's value, the child
instance for that name successfully validates against the
corresponding schema.

Omitting this keyword has the same behavior as an empty object.

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

这意味着,properties 对象中某个键值的架构适用于 JSON 实例中的相应值。

以你为例:

...    
    "properties" : {
            "pltfrm_nm": {"type" : "string"}
    }
...

pltfrm_nm 必须是字符串。

这是上述代码片段暗示的唯一约束。

您是否认为 proprties 中未列出的键会导致验证错误? 如果是这样,您需要具体说明这一事实。

要指定除 properties 中定义的属性之外的其他属性,您需要使用 additionalProperties

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

我建议您查看 http://json-schema.org/understanding-json-schema/ and http://json-schema.org/understanding-json-schema/ 上的学习资源,了解 JSON 模式。

documentation 状态:

By default, providing additional properties is valid:

The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword. By default any additional properties are allowed.

The additionalProperties keyword may be either a boolean or an object. If additionalProperties is a boolean and set to false, no additional properties will be allowed.

Reusing the example above, but this time setting additionalProperties to false.

所以尝试将其添加到您的 query_schema:

query_schema = {
    "type": "object",
    "properties" : {
        "pltfrm_nm": {"type" : "string"},
        "srvr_nm": {"type": "string"},
        "db_nm": {"type": "string"},
        "tbl_nm": {"type": "string"},
        "ip_addr_id": {"type": "string"},
        "usr_id": {"type": "string"},
        "sql_txt": {"type": "string"},
        "timestmp": {"type": "string"},
    },
  "additionalProperties": False
}