Schema should be optional

This commit is contained in:
Jeremy Stretch 2025-03-28 15:20:27 -04:00
parent 7edc67ed8a
commit 1be9209b04
3 changed files with 11 additions and 7 deletions

View File

@ -409,6 +409,7 @@ class DeviceTypeForm(NetBoxModelForm):
class ModuleTypeProfileForm(NetBoxModelForm): class ModuleTypeProfileForm(NetBoxModelForm):
schema = JSONField( schema = JSONField(
label=_('Schema'), label=_('Schema'),
required=False,
help_text=_("Enter a valid JSON schema to define supported attributes.") help_text=_("Enter a valid JSON schema to define supported attributes.")
) )
comments = CommentField() comments = CommentField()

View File

@ -25,7 +25,7 @@ class Migration(migrations.Migration):
('description', models.CharField(blank=True, max_length=200)), ('description', models.CharField(blank=True, max_length=200)),
('comments', models.TextField(blank=True)), ('comments', models.TextField(blank=True)),
('name', models.CharField(max_length=100, unique=True)), ('name', models.CharField(max_length=100, unique=True)),
('schema', models.JSONField()), ('schema', models.JSONField(blank=True, null=True)),
('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')), ('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')),
], ],
options={ options={

View File

@ -33,6 +33,8 @@ class ModuleTypeProfile(PrimaryModel):
unique=True unique=True
) )
schema = models.JSONField( schema = models.JSONField(
blank=True,
null=True,
verbose_name=_('schema') verbose_name=_('schema')
) )
@ -50,12 +52,13 @@ class ModuleTypeProfile(PrimaryModel):
super().clean() super().clean()
# Validate the schema definition # Validate the schema definition
try: if self.schema:
JSONSchemaValidator.check_schema(self.schema) try:
except SchemaError as e: JSONSchemaValidator.check_schema(self.schema)
raise ValidationError({ except SchemaError as e:
'schema': _("Invalid schema: {error}").format(error=e) raise ValidationError({
}) 'schema': _("Invalid schema: {error}").format(error=e)
})
class ModuleType(ImageAttachmentsMixin, PrimaryModel, WeightMixin): class ModuleType(ImageAttachmentsMixin, PrimaryModel, WeightMixin):