Support choices & default values

This commit is contained in:
Jeremy Stretch 2025-03-26 17:26:33 -04:00
parent 0d2a8b8b85
commit dc46cb26d1

View File

@ -82,10 +82,15 @@ class JSONSchemaProperty:
'label': self.title or title(name), 'label': self.title or title(name),
'help_text': self.description, 'help_text': self.description,
'required': required, 'required': required,
'initial': self.default,
} }
# Choices
if self.enum:
field_kwargs['choices'] = [(v, v) for v in self.enum]
# String validation # String validation
if self.type == PropertyTypeEnum.STRING: elif self.type == PropertyTypeEnum.STRING:
if self.minLength is not None: if self.minLength is not None:
field_kwargs['min_length'] = self.minLength field_kwargs['min_length'] = self.minLength
if self.maxLength is not None: if self.maxLength is not None:
@ -113,6 +118,10 @@ class JSONSchemaProperty:
""" """
Resolve the property's type (and string format, if specified) to the appropriate field class. Resolve the property's type (and string format, if specified) to the appropriate field class.
""" """
if self.enum:
if self.type == PropertyTypeEnum.ARRAY.value:
return forms.MultipleChoiceField
return forms.ChoiceField
if self.type == PropertyTypeEnum.STRING.value and self.format is not None: if self.type == PropertyTypeEnum.STRING.value and self.format is not None:
try: try:
return STRING_FORM_FIELDS[self.format] return STRING_FORM_FIELDS[self.format]