如何使用 JSL python 库定义字符串到字符串 JSON 模式的映射?

How to define map of string to string JSON schema using JSL python library?

假设在我的 JSON 文档的属性中,其中一个包含 HTTP headers 的 collection,它只是字符串键到字符串值的映射。

{
  "property": "value",
  "headers": {
    "Content-Type": "text/css",
    "Last-Modified": "Tue, 08 Aug 2017 18:57:23 GMT",
    "Etag": "123456abc"
  }
}

如何定义此类文档的 JSON 架构 JSL Python library hopefully achieving something similar to 关于如何定义字符串到整数的映射。

此外,我真的很想对结果 JSON 模式进行解释(类似于上述答案中显示的内容),因为我无法清楚地理解它。

JSL 库为您希望定义对象 (dictionary/map) 和通过 “additional_properties”

描述值类型

举个例子:

>>> import jsl
... 
... class PayloadSchema(jsl.Document):
...     ip_address = jsl.IPv4Field(required=True)
...     http_headers = jsl.DictField(required=True, additional_properties=jsl.StringField(), min_properties=1)
... 
>>> PayloadSchema.get_schema()

这将产生以下 JSON 架构(草稿 4):

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "ip_address": {
      "type": "string",
      "format": "ipv4"
    },
    "http_headers": {
      "type": "object",
      "additionalProperties": {
        "type": "string"
      },
      "minProperties": 1
    }
  },
  "required": [
    "ip_address",
    "http_headers"
  ],
  "additionalProperties": false
}