Package: warchant_dc_schema
dc_schemaTiny library to generateJSON schema(2020-12) from pythondataclasses. No other dependencies, standard library only.pip install dc-schemaFork changelog:0.0.10:Add ability to definepatternPropertiesfor dataclasses. See./tests/test_dc_schema.pyfor details (test_object_pattern_properties).0.0.9:Add handling oftyping.AnytypeAdd ability to defineadditionalPropertiesfor dataclasses. See./tests/test_dc_schema.pyfor details (test_schema_additiona_properties_are_not_allowed).Replace flake8, isort, black with ruff+ruff format.Add .pre-commit-config.yaml with pre-commit hooks: ruff, ruff-format, mypy...If a dataclass hasSchemaConfigattribute and doesn't haveannotationattribute, raise aValueError.Assumptionspython 3.9+MotivationCreate a lightweight, focused solution to generate JSON schema from plain dataclasses.pydanticis 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.UsageBasicsCreate a regular python dataclass and pass it toget_schema.importdataclassesimportdatetimeimportjsonfromdc_schemaimportget_schema@dataclasses.dataclassclassBook:title:strpublished:bool=False@dataclasses.dataclassclassAuthor:name:strage:intdob:datetime.datebooks: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"]}}}AnnotationsYou can usetyping.Annotated+SchemaAnnotationto attach
metadata to the schema, such as field descriptions, examples, validation (min/max length, regex pattern, ...), etc.
Consultthe codefor full details.importdataclassesimportdatetimeimportjsonimporttypingastfromdc_schemaimportget_schema,SchemaAnnotation@dataclasses.dataclassclassAuthor: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"])]=Noneprint(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 aSchemaConfig.importdataclassesimportjsonfromdc_schemaimportget_schema,SchemaAnnotation@dataclasses.dataclassclassUser:name:strclassSchemaConfig: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 examplesSee thetestsfor full example usage.CLIdc_schema <file_path> <dataclass>e.g.dc_schema ./schema.py AuthorOther toolsFor working with dataclasses or JSON schema:https://github.com/konradhalas/dacite- create data classes from dictionaries.https://python-jsonschema.readthedocs.io/en/stable/- validate an object against a JSON schema.https://json-schema.org/understanding-json-schema/index.html- nice reference for understanding JSON schema.