force separate dict validator usage
This commit is contained in:
parent
021ed500aa
commit
319cf1a3bb
3 changed files with 14 additions and 32 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
from typing import Dict
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from typing import Set
|
from typing import Set
|
||||||
from typing import Type
|
from typing import Type
|
||||||
|
|
@ -17,15 +18,13 @@ class DictValidator(Validator):
|
||||||
|
|
||||||
required_fields: Set[str] = set()
|
required_fields: Set[str] = set()
|
||||||
optional_fields: Set[str] = set()
|
optional_fields: Set[str] = set()
|
||||||
allow_extra_fields = False
|
|
||||||
|
|
||||||
def __validate_required_fields_are_present(self):
|
_allow_extra_fields = False
|
||||||
"""
|
|
||||||
Raises
|
def __init__(self, name, value):
|
||||||
-------
|
super().__init__(name, value)
|
||||||
ValidationException
|
|
||||||
If the required fields are not present in the dict
|
# Ensure all required keys are present
|
||||||
"""
|
|
||||||
for required_key in self.required_fields:
|
for required_key in self.required_fields:
|
||||||
if required_key not in self.value:
|
if required_key not in self.value:
|
||||||
error_msg = (
|
error_msg = (
|
||||||
|
|
@ -33,30 +32,17 @@ class DictValidator(Validator):
|
||||||
)
|
)
|
||||||
raise ValidationException(error_msg)
|
raise ValidationException(error_msg)
|
||||||
|
|
||||||
def __validate_extra_fields(self):
|
# If no extra fields are allowed, ensure all fields are either
|
||||||
"""
|
# required or optional fields
|
||||||
Raises
|
if not self._allow_extra_fields:
|
||||||
-------
|
|
||||||
ValidationException
|
|
||||||
If allow_extra_fields=False and non-required/options fields are present
|
|
||||||
"""
|
|
||||||
if not self.allow_extra_fields:
|
|
||||||
for object_key in self.object_keys:
|
for object_key in self.object_keys:
|
||||||
if (
|
if object_key not in self.allowed_fields:
|
||||||
object_key not in self.required_fields
|
|
||||||
and object_key not in self.optional_fields
|
|
||||||
):
|
|
||||||
error_msg = (
|
error_msg = (
|
||||||
f"'{self.name}' contains the field '{object_key}' which is not allowed. "
|
f"'{self.name}' contains the field '{object_key}' which is not allowed. "
|
||||||
f"Allowed fields: {', '.join(self.allowed_fields)}"
|
f"Allowed fields: {', '.join(self.allowed_fields)}"
|
||||||
)
|
)
|
||||||
raise ValidationException(error_msg)
|
raise ValidationException(error_msg)
|
||||||
|
|
||||||
def __init__(self, name, value):
|
|
||||||
super().__init__(name, value)
|
|
||||||
self.__validate_required_fields_are_present()
|
|
||||||
self.__validate_extra_fields()
|
|
||||||
|
|
||||||
def validate_dict_value(
|
def validate_dict_value(
|
||||||
self, dict_value_name: str, validator: Type[T], default: Optional[Any] = None
|
self, dict_value_name: str, validator: Type[T], default: Optional[Any] = None
|
||||||
) -> T:
|
) -> T:
|
||||||
|
|
@ -92,4 +78,4 @@ class DictValidator(Validator):
|
||||||
|
|
||||||
|
|
||||||
class DictWithExtraFieldsValidator(DictValidator):
|
class DictWithExtraFieldsValidator(DictValidator):
|
||||||
allow_extra_fields = True
|
_allow_extra_fields = True
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,6 @@ class PresetValidator(DictValidator):
|
||||||
"overrides",
|
"overrides",
|
||||||
*SubscriptionSourceName.all(),
|
*SubscriptionSourceName.all(),
|
||||||
}
|
}
|
||||||
allow_extra_fields = False
|
|
||||||
|
|
||||||
def __init__(self, name: str, value: Any):
|
def __init__(self, name: str, value: Any):
|
||||||
super().__init__(name=name, value=value)
|
super().__init__(name=name, value=value)
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ from typing import Dict
|
||||||
from typing import Type
|
from typing import Type
|
||||||
|
|
||||||
from ytdl_subscribe.validators.base.dict_validator import DictValidator
|
from ytdl_subscribe.validators.base.dict_validator import DictValidator
|
||||||
|
from ytdl_subscribe.validators.base.dict_validator import DictWithExtraFieldsValidator
|
||||||
from ytdl_subscribe.validators.base.string_validator import StringValidator
|
from ytdl_subscribe.validators.base.string_validator import StringValidator
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -11,14 +12,10 @@ class DownloadStrategyValidator(DictValidator):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class SourceValidator(DictValidator):
|
class SourceValidator(DictWithExtraFieldsValidator):
|
||||||
# All media sources must define a download strategy
|
# All media sources must define a download strategy
|
||||||
required_fields = {"download_strategy"}
|
required_fields = {"download_strategy"}
|
||||||
|
|
||||||
# We allow extra fields at this level, once the download strategy is chosen,
|
|
||||||
# all fields in the dict should be required.
|
|
||||||
allow_extra_fields = True
|
|
||||||
|
|
||||||
download_strategy_validator_mapping: Dict[str, Type[DownloadStrategyValidator]] = {}
|
download_strategy_validator_mapping: Dict[str, Type[DownloadStrategyValidator]] = {}
|
||||||
|
|
||||||
def __init__(self, name: str, value: Any):
|
def __init__(self, name: str, value: Any):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue