{"id":15013042,"url":"https://github.com/peter554/dc_schema","last_synced_at":"2025-04-12T04:20:57.230Z","repository":{"id":41973515,"uuid":"483542351","full_name":"Peter554/dc_schema","owner":"Peter554","description":"Generate JSON schema from python dataclasses","archived":false,"fork":false,"pushed_at":"2024-02-13T20:47:50.000Z","size":56,"stargazers_count":15,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-14T06:13:11.107Z","etag":null,"topics":["dataclasses","jsonschema","jsonschema-generator","python"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/dc-schema/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Peter554.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-20T06:58:28.000Z","updated_at":"2024-04-09T10:08:20.000Z","dependencies_parsed_at":"2024-09-11T23:59:35.380Z","dependency_job_id":null,"html_url":"https://github.com/Peter554/dc_schema","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Peter554%2Fdc_schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Peter554%2Fdc_schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Peter554%2Fdc_schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Peter554%2Fdc_schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Peter554","download_url":"https://codeload.github.com/Peter554/dc_schema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248514395,"owners_count":21116952,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dataclasses","jsonschema","jsonschema-generator","python"],"created_at":"2024-09-24T19:43:38.944Z","updated_at":"2025-04-12T04:20:57.210Z","avatar_url":"https://github.com/Peter554.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# dc_schema\n\n[![CI](https://github.com/Peter554/dc_schema/actions/workflows/ci.yaml/badge.svg)](https://github.com/Peter554/dc_schema/actions/workflows/ci.yaml)\n[![codecov](https://codecov.io/gh/Peter554/dc_schema/branch/master/graph/badge.svg?token=YLT3N0HWO9)](https://codecov.io/gh/Peter554/dc_schema)\n\nTiny library to generate [JSON schema](https://json-schema.org/) (2020-12) from python \n[dataclasses](https://docs.python.org/3/library/dataclasses.html). No other dependencies, standard library only.\n\n```\npip install dc-schema \n```\n\n## Assumptions\n\n* python 3.9+ \n\n## Motivation\n\nCreate a lightweight, focused solution to generate JSON schema from plain dataclasses. [pydantic](https://pydantic-docs.helpmanual.io/) 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.\n\n## Usage\n\n### Basics\n\nCreate a regular python dataclass and pass it to `get_schema`.\n\n```py\nimport dataclasses\nimport datetime\nimport json\n\nfrom dc_schema import get_schema\n\n@dataclasses.dataclass\nclass Book:\n    title: str\n    published: bool = False\n\n@dataclasses.dataclass\nclass Author:\n    name: str\n    age: int\n    dob: datetime.date\n    books: list[Book]\n\nprint(json.dumps(get_schema(Author), indent=2))\n```\n\n```json\n{\n  \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n  \"type\": \"object\",\n  \"title\": \"Author\",\n  \"properties\": {\n    \"name\": {\n      \"type\": \"string\"\n    },\n    \"age\": {\n      \"type\": \"integer\"\n    },\n    \"dob\": {\n      \"type\": \"string\",\n      \"format\": \"date\"\n    },\n    \"books\": {\n      \"type\": \"array\",\n      \"items\": {\n        \"allOf\": [\n          {\n            \"$ref\": \"#/$defs/Book\"\n          }\n        ]\n      }\n    }\n  },\n  \"required\": [\n    \"name\",\n    \"age\",\n    \"dob\",\n    \"books\"\n  ],\n  \"$defs\": {\n    \"Book\": {\n      \"type\": \"object\",\n      \"title\": \"Book\",\n      \"properties\": {\n        \"title\": {\n          \"type\": \"string\"\n        },\n        \"published\": {\n          \"type\": \"boolean\",\n          \"default\": false\n        }\n      },\n      \"required\": [\n        \"title\"\n      ]\n    }\n  }\n}\n```\n\n### Annotations\n\nYou can use [typing.Annotated](https://docs.python.org/3/library/typing.html#typing.Annotated) + `SchemaAnnotation` to attach\nmetadata to the schema, such as field descriptions, examples, validation (min/max length, regex pattern, ...), etc. \nConsult [the code](https://github.com/Peter554/dc_schema/blob/master/dc_schema/__init__.py) for full details.\n\n```py\nimport dataclasses\nimport datetime\nimport json\nimport typing as t\n\nfrom dc_schema import get_schema, SchemaAnnotation\n\n@dataclasses.dataclass\nclass Author:\n    name: t.Annotated[str, SchemaAnnotation(title=\"Full name\", description=\"The authors full name\")]\n    age: t.Annotated[int, SchemaAnnotation(minimum=0)]\n    dob: t.Annotated[t.Optional[datetime.date], SchemaAnnotation(examples=[\"1990-01-17\"])] = None\n\nprint(json.dumps(get_schema(Author), indent=2))\n```\n\n```json\n{\n  \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n  \"type\": \"object\",\n  \"title\": \"Author\",\n  \"properties\": {\n    \"name\": {\n      \"type\": \"string\",\n      \"title\": \"Full name\",\n      \"description\": \"The authors full name\"\n    },\n    \"age\": {\n      \"type\": \"integer\",\n      \"minimum\": 0\n    },\n    \"dob\": {\n      \"anyOf\": [\n        {\n          \"type\": \"string\",\n          \"format\": \"date\"\n        },\n        {\n          \"type\": \"null\"\n        }\n      ],\n      \"default\": null,\n      \"examples\": [\n        \"1990-01-17\"\n      ]\n    }\n  },\n  \"required\": [\n    \"name\",\n    \"age\"\n  ]\n}\n```\n\nTo customize the metadata of a dataclass itself, use a `SchemaConfig`.\n\n```py\nimport dataclasses\nimport json\n\nfrom dc_schema import get_schema, SchemaAnnotation\n\n@dataclasses.dataclass\nclass User:\n    name: str\n\n    class SchemaConfig:\n        annotation = SchemaAnnotation(title=\"System user\", description=\"A user of the system\")\n\nprint(json.dumps(get_schema(User), indent=2))\n```\n\n```json\n{\n  \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n  \"type\": \"object\",\n  \"title\": \"System user\",\n  \"description\": \"A user of the system\",\n  \"properties\": {\n    \"name\": {\n      \"type\": \"string\"\n    }\n  },\n  \"required\": [\n    \"name\"\n  ]\n}\n```\n\n### Further examples\n\nSee the [tests](https://github.com/Peter554/dc_schema/blob/master/tests/test_dc_schema.py) for full example usage.\n\n## CLI\n\n```\ndc_schema \u003cfile_path\u003e \u003cdataclass\u003e\n```\n\ne.g.\n\n```\ndc_schema ./schema.py Author\n```\n\n## Other tools\n\nFor working with dataclasses or JSON schema:\n\n* https://github.com/konradhalas/dacite - create data classes from dictionaries.\n* https://python-jsonschema.readthedocs.io/en/stable/ - validate an object against a JSON schema.\n* https://json-schema.org/understanding-json-schema/index.html - nice reference for understanding JSON schema. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeter554%2Fdc_schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpeter554%2Fdc_schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeter554%2Fdc_schema/lists"}