diff --git a/django_forms_as_jsonschema/jsonschema.py b/django_forms_as_jsonschema/jsonschema.py index a76a9be2e70e410adc00e42656ff3a22fbad5db7..966c0432078f5f96dc198320a7c79f9023cf42e6 100644 --- a/django_forms_as_jsonschema/jsonschema.py +++ b/django_forms_as_jsonschema/jsonschema.py @@ -67,36 +67,37 @@ class JSONSchema: # } def add_field(self, name, field): - type_ = "string" - format_ = None - x_display = None - enum = None + new_field = { + "type": "string", + "title": str(field.label), + "description": str(field.help_text), + } # string, number, integer, boolean. match type(field.widget): case forms.TextInput: - type_ = "string" + new_field["type"] = "string" case forms.NumberInput: - type_ = "number" + new_field["type"] = "integer" if type(field) == forms.IntegerField else "number" case forms.EmailInput: - type_ = "string" - format_ = "email" + new_field["type"] = "string" + new_field["format"] = "email" case forms.URLInput: - type_ = "string" - format_ = "url" + new_field["type"] = "string" + new_field["format"] = "url" case forms.PasswordInput: - type_ = "string" - format_ = "password" - x_display = "password" + new_field["type"] = "string" + new_field["format"] = "password" + new_field["x-display"] = "password" case forms.HiddenInput: - type_ = "string" - format_ = "hidden" + new_field["type"] = "string" + new_field["format"] = "hidden" case forms.MultipleHiddenInput: ... @@ -108,26 +109,26 @@ class JSONSchema: ... case forms.Textarea: - x_display = "textarea" + new_field["x-display"] = "textarea" case forms.DateInput: - type_ = "string" - format_ = "date" + new_field["type"] = "string" + new_field["format"] = "date" case forms.DateTimeInput: - type_ = "string" - format_ = "date-time" + new_field["type"] = "string" + new_field["format"] = "date-time" case forms.TimeInput: - type_ = "string" - format_ = "time" + new_field["type"] = "string" + new_field["format"] = "time" case forms.CheckboxInput: - type_ = "boolean" + new_field["type"] = "boolean" case forms.Select: - type_ = "string" - enum = [] # Fixme: load data from widget.options(…) + new_field["type"] = "string" + new_field["enum"] = [] # Fixme: load data from widget.options(…) case forms.NullBooleanSelect: ... @@ -150,14 +151,4 @@ class JSONSchema: case forms.SelectDateWidget: ... - self.schema["properties"][name] = { - "type": type_, - "title": str(field.label), - "description": str(field.help_text), - } - if x_display: - self.schema["properties"][name]["x-display"] = x_display - if format_: - self.schema["properties"][name]["format"] = format_ - if enum: - self.schema["properties"][name]["enum"] = enum + self.schema["properties"][name] = new_field