Skip to main content

dc_schema

CI codecov

Tiny library to generate JSON schema (2020-12) from python dataclasses. No other dependencies, standard library only.

pip install dc-schema 

Assumptions

  • python 3.9+

Motivation

Create a lightweight, focused solution to generate JSON schema from plain dataclasses. pydantic is a much more mature option, however it also does a lot of other things I didn't want to include here. Deepen my understanding of python dataclasses, typing and JSON schema.

Usage

Basics

Create a regular python dataclass and pass it to get_schema.

import dataclasses
import datetime
import json

from dc_schema import get_schema

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

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

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 + SchemaAnnotation to attach metadata to the schema, such as field descriptions, examples, validation (min/max length, regex pattern, ...), etc. Consult the code for full details.

import dataclasses
import datetime
import json
import typing as t

from dc_schema import get_schema, SchemaAnnotation

@dataclasses.dataclass
class Author:
    name: t.Annotated[str, SchemaAnnotation(title="Full name", description="The authors full name")]
    age: t.Annotated[int, SchemaAnnotation(minimum=0)]
    dob: t.Annotated[t.Optional[datetime.date], SchemaAnnotation(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"
  ]
}

To customize the metadata of a dataclass itself, use a SchemaConfig.

import dataclasses
import json

from dc_schema import get_schema, SchemaAnnotation

@dataclasses.dataclass
class User:
    name: str

    class SchemaConfig:
        annotation = SchemaAnnotation(title="System user", description="A user of the system")

print(json.dumps(get_schema(User), indent=2))
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "title": "System user",
  "description": "A user of the system",
  "properties": {
    "name": {
      "type": "string"
    }
  },
  "required": [
    "name"
  ]
}

Further examples

See the tests for full example usage.

CLI

dc_schema <file_path> <dataclass>

e.g.

dc_schema ./schema.py Author

Other tools

For working with dataclasses or JSON schema:

Metadata

Release files for dc-schema 0.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dc-schema 0.0.8
File Size Uploaded
dc_schema-0.0.8.tar.gz 6.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dc-schema 0.0.8
File Interpreter ABI Platform
dc_schema-0.0.8-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / dc_schema-0.0.8.tar.gz

Download URL dc_schema-0.0.8.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
89fed792c63b80a91d4fef723f9aefbca83952eb611fbc168ef8381bf62acbb5
BLAKE2b-256 checksum
How to use checksums
b521bdff5fc8ae03453b23e753a79ebfcfa7e44b9ff40e5a7d364e2456a997ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.10.0 Linux/5.13.0-39-generic

Release files / dc_schema-0.0.8-py3-none-any.whl

Download URL dc_schema-0.0.8-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c5990a2ce8fa22ccf18789b11db2440eeddcd36727cd7d3b6c802366d92ef17a
BLAKE2b-256 checksum
How to use checksums
484c69eb48bd94bbb9da06f5c019e951d3f07c14e1f3cec7fbf950f1d2536728
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.10.0 Linux/5.13.0-39-generic

Release history Release notifications | RSS feed

This release

0.0.8 This release

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page