diff --git a/django_forms_as_jsonschema/forms.py b/django_forms_as_jsonschema/forms.py index 207a662457e4df490f8c9bd5a603f975abe3f331..570b1fc35b52ac99d7d9af31b4eb3aeeb6caf350 100644 --- a/django_forms_as_jsonschema/forms.py +++ b/django_forms_as_jsonschema/forms.py @@ -4,13 +4,19 @@ from .jsonschema import JSONSchema class JSONSchemaFormMixin: + """Mixin to extend Django forms with a converter into a JSON schema.""" layout = None def as_jsonschema(self: Form) -> dict: + """Encode form fields and layout into a JSON schema.""" schema = JSONSchema() + if self.layout is None: + # If no layout is provided, simply add all fields as individual rows for name, field in self.fields.items(): schema.add_field(name, field) else: + # Offload generation to the layout class schema.update_properties(self.layout.build_schema(schema, self.fields)["properties"]) + return schema.schema