mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-27 02:48:38 -06:00
Initial work on JSON schema validation
This commit is contained in:
parent
678e6f1bd5
commit
69e67d0258
@ -406,6 +406,9 @@ class DeviceTypeForm(NetBoxModelForm):
|
|||||||
|
|
||||||
|
|
||||||
class ModuleTypeProfileForm(NetBoxModelForm):
|
class ModuleTypeProfileForm(NetBoxModelForm):
|
||||||
|
schema = JSONField(
|
||||||
|
label=_('Schema')
|
||||||
|
)
|
||||||
comments = CommentField()
|
comments = CommentField()
|
||||||
|
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
|
import jsonschema
|
||||||
import yaml
|
import yaml
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models.signals import post_save
|
from django.db.models.signals import post_save
|
||||||
from django.utils.translation import gettext_lazy as _
|
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.choices import *
|
||||||
from dcim.utils import update_interface_bridges
|
from dcim.utils import update_interface_bridges
|
||||||
@ -42,6 +45,17 @@ class ModuleTypeProfile(PrimaryModel):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
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):
|
class ModuleType(ImageAttachmentsMixin, PrimaryModel, WeightMixin):
|
||||||
"""
|
"""
|
||||||
@ -108,6 +122,20 @@ class ModuleType(ImageAttachmentsMixin, PrimaryModel, WeightMixin):
|
|||||||
def full_name(self):
|
def full_name(self):
|
||||||
return f"{self.manufacturer} {self.model}"
|
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):
|
def to_yaml(self):
|
||||||
data = {
|
data = {
|
||||||
'profile': self.profile.name if self.profile else None,
|
'profile': self.profile.name if self.profile else None,
|
||||||
|
Loading…
Reference in New Issue
Block a user