mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-27 02:48:38 -06:00
Implement sanity checking for JSON schema definitions
This commit is contained in:
parent
f1092d3bc6
commit
ef43bdfe65
@ -4,8 +4,7 @@ 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.exceptions import 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
|
||||||
@ -13,6 +12,7 @@ from extras.models import ConfigContextModel, CustomField
|
|||||||
from netbox.models import PrimaryModel
|
from netbox.models import PrimaryModel
|
||||||
from netbox.models.features import ImageAttachmentsMixin
|
from netbox.models.features import ImageAttachmentsMixin
|
||||||
from netbox.models.mixins import WeightMixin
|
from netbox.models.mixins import WeightMixin
|
||||||
|
from utilities.jsonschema import validate_schema
|
||||||
from utilities.string import title
|
from utilities.string import title
|
||||||
from .device_components import *
|
from .device_components import *
|
||||||
|
|
||||||
@ -52,12 +52,12 @@ class ModuleTypeProfile(PrimaryModel):
|
|||||||
super().clean()
|
super().clean()
|
||||||
|
|
||||||
# Validate the schema definition
|
# Validate the schema definition
|
||||||
if self.schema:
|
if self.schema is not None:
|
||||||
try:
|
try:
|
||||||
JSONSchemaValidator.check_schema(self.schema)
|
validate_schema(self.schema)
|
||||||
except SchemaError as e:
|
except ValidationError as e:
|
||||||
raise ValidationError({
|
raise ValidationError({
|
||||||
'schema': _("Invalid schema: {error}").format(error=e)
|
'schema': e.message,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,7 +4,11 @@ from typing import Any
|
|||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.contrib.postgres.forms import SimpleArrayField
|
from django.contrib.postgres.forms import SimpleArrayField
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
from django.core.validators import RegexValidator
|
from django.core.validators import RegexValidator
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
from jsonschema.exceptions import SchemaError
|
||||||
|
from jsonschema.validators import validator_for
|
||||||
|
|
||||||
from utilities.string import title
|
from utilities.string import title
|
||||||
from utilities.validators import MultipleOfValidator
|
from utilities.validators import MultipleOfValidator
|
||||||
@ -13,6 +17,7 @@ __all__ = (
|
|||||||
'JSONSchemaProperty',
|
'JSONSchemaProperty',
|
||||||
'PropertyTypeEnum',
|
'PropertyTypeEnum',
|
||||||
'StringFormatEnum',
|
'StringFormatEnum',
|
||||||
|
'validate_schema',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -143,3 +148,19 @@ class JSONSchemaProperty:
|
|||||||
return FORM_FIELDS[self.type]
|
return FORM_FIELDS[self.type]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise ValueError(f"Unknown property type: {self.type}")
|
raise ValueError(f"Unknown property type: {self.type}")
|
||||||
|
|
||||||
|
|
||||||
|
def validate_schema(schema):
|
||||||
|
"""
|
||||||
|
Check that a minimum JSON schema definition is defined.
|
||||||
|
"""
|
||||||
|
# Provide some basic sanity checking (not provided by jsonschema)
|
||||||
|
if not schema or type(schema) is not dict:
|
||||||
|
raise ValidationError(_("Invalid JSON schema definition"))
|
||||||
|
if not schema.get('properties'):
|
||||||
|
raise ValidationError(_("JSON schema must define properties"))
|
||||||
|
try:
|
||||||
|
ValidatorClass = validator_for(schema)
|
||||||
|
ValidatorClass.check_schema(schema)
|
||||||
|
except SchemaError as e:
|
||||||
|
raise ValidationError(_("Invalid JSON schema definition: {error}").format(error=e))
|
||||||
|
Loading…
Reference in New Issue
Block a user