Skip to main content

Generate JSON schema from python dataclasses

Project description

dc_schema

Generate JSON schema (2020-12) from python dataclasses. No dependencies, standard library only.

pip install dc_schema 

Assumptions

Usage

Basics

Create a regular python dataclass and pass it to get_schema.

from __future__ import annotations

import dataclasses
import datetime
import json

from dc_schema import get_schema


@dataclasses.dataclass
class Author:
    name: str
    age: int
    dob: datetime.date
    books: list[Book]


@dataclasses.dataclass
class Book:
    title: str
    published: bool = False


print(json.dumps(get_schema(Author), indent=2))
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "title": "Author",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer"
    },
    "dob": {
      "type": "string",
      "format": "date"
    },
    "books": {
      "type": "array",
      "items": {
        "allOf": [
          {
            "$ref": "#/$defs/Book"
          }
        ]
      }
    }
  },
  "required": [
    "name",
    "age",
    "dob",
    "books"
  ],
  "$defs": {
    "Book": {
      "type": "object",
      "title": "Book",
      "properties": {
        "title": {
          "type": "string"
        },
        "published": {
          "type": "boolean",
          "default": false
        }
      },
      "required": [
        "title"
      ]
    }
  }
}

Annotations

You can use typing.Annotated + annotation to attach metadata to the schema, such as field descriptions, examples, validation (min/max length, regex pattern, ...), etc. Consult the code for full details.

from __future__ import annotations

import dataclasses
import datetime
import json
import typing as t

from dc_schema import get_schema, annotation


@dataclasses.dataclass
class Author:
    name: t.Annotated[str, annotation(title="Full name", description="The authors full name")]
    age: t.Annotated[int, annotation(minimum=0)]
    dob: t.Annotated[t.Optional[datetime.date], annotation(examples=["1990-01-17"])] = None


print(json.dumps(get_schema(Author), indent=2))
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "title": "Author",
  "properties": {
    "name": {
      "type": "string",
      "title": "Full name",
      "description": "The authors full name"
    },
    "age": {
      "type": "integer",
      "minimum": 0
    },
    "dob": {
      "anyOf": [
        {
          "type": "string",
          "format": "date"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "examples": [
        "1990-01-17"
      ]
    }
  },
  "required": [
    "name",
    "age"
  ]
}

Further examples

See the tests for full example usage.

Other tools

For working with dataclasses or JSON schema:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dc_schema-0.0.2.tar.gz (5.2 kB view hashes)

Uploaded Source

Built Distribution

dc_schema-0.0.2-py3-none-any.whl (5.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page