Move backend type validation from serializer to model

This commit is contained in:
Jeremy Stretch 2023-10-24 11:19:16 -04:00
parent 409903d017
commit 6e58c480ee
2 changed files with 6 additions and 9 deletions

View File

@ -1,5 +1,3 @@
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers
from core.choices import *
@ -41,13 +39,6 @@ class DataSourceSerializer(NetBoxModelSerializer):
'parameters', 'ignore_rules', 'created', 'last_updated', 'file_count',
]
def clean(self):
if self.type and self.type not in get_data_backend_choices():
raise ValidationError({
'type': _("Unknown backend type: {type}".format(type=self.type))
})
class DataFileSerializer(NetBoxModelSerializer):
url = serializers.HyperlinkedIdentityField(

View File

@ -118,6 +118,12 @@ class DataSource(JobsMixin, PrimaryModel):
def clean(self):
# Validate data backend type
if self.type and self.type not in registry['data_backends']:
raise ValidationError({
'type': _("Unknown backend type: {type}".format(type=self.type))
})
# Ensure URL scheme matches selected type
if self.backend_class.is_local and self.url_scheme not in ('file', ''):
raise ValidationError({