Initial work on JSON schema validation

This commit is contained in:
Jeremy Stretch 2025-03-25 17:11:36 -04:00
parent 678e6f1bd5
commit 69e67d0258
2 changed files with 31 additions and 0 deletions

View File

@ -406,6 +406,9 @@ class DeviceTypeForm(NetBoxModelForm):
class ModuleTypeProfileForm(NetBoxModelForm):
schema = JSONField(
label=_('Schema')
)
comments = CommentField()
fieldsets = (

View File

@ -1,8 +1,11 @@
import jsonschema
import yaml
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models.signals import post_save
from django.utils.translation import gettext_lazy as _
from jsonschema.exceptions import SchemaError, ValidationError as JSONValidationError
from jsonschema.validators import Draft202012Validator as JSONSchemaValidator
from dcim.choices import *
from dcim.utils import update_interface_bridges
@ -42,6 +45,17 @@ class ModuleTypeProfile(PrimaryModel):
def __str__(self):
return self.name
def clean(self):
super().clean()
# Validate the schema definition
try:
JSONSchemaValidator.check_schema(self.schema)
except SchemaError as e:
raise ValidationError({
'schema': _("Invalid schema: {error}").format(error=e)
})
class ModuleType(ImageAttachmentsMixin, PrimaryModel, WeightMixin):
"""
@ -108,6 +122,20 @@ class ModuleType(ImageAttachmentsMixin, PrimaryModel, WeightMixin):
def full_name(self):
return f"{self.manufacturer} {self.model}"
def clean(self):
super().clean()
# Validate any attributes against the assigned profile's schema
if self.profile:
try:
jsonschema.validate(self.attributes, schema=self.profile.schema)
except JSONValidationError as e:
raise ValidationError({
'attributes': _("Invalid schema: {error}").format(error=e)
})
else:
self.attributes = None
def to_yaml(self):
data = {
'profile': self.profile.name if self.profile else None,