0

Let’s say among properties of my JSON document one of them holds a collection of HTTP headers which is simply a map of string key to a string value.

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

How to define a JSON schema of such document using JSL Python library hopefully achieving something similar to this answer on how to define a map of string to an integer.

Also, I would really like to have an explanation of the resulted JSON schema (similarly to what was shown in the mentioned answer) as I am unable to clearly comprehend it.

1 Answer 1

2

JSL library provides a DictField class type for such cases when you wish to define an object (dictionary/map) and describe values type via “additional_properties”

For an example:

>>> 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()

This will produce following JSON schema (draft 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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.