[DEV] String validators to allow ints, floats, bools
This commit is contained in:
parent
395691f2b9
commit
ecffa1fcc5
3 changed files with 11 additions and 6 deletions
|
|
@ -10,7 +10,7 @@ from yt_dlp.utils import sanitize_filename
|
|||
from ytdl_sub.utils.exceptions import InvalidVariableNameException
|
||||
from ytdl_sub.utils.exceptions import StringFormattingException
|
||||
from ytdl_sub.utils.exceptions import StringFormattingVariableNotFoundException
|
||||
from ytdl_sub.validators.validators import ListValidator
|
||||
from ytdl_sub.validators.validators import ListValidator, StringValidator
|
||||
from ytdl_sub.validators.validators import LiteralDictValidator
|
||||
from ytdl_sub.validators.validators import Validator
|
||||
|
||||
|
|
@ -47,7 +47,7 @@ def is_valid_source_variable_name(input_str: str, raise_exception: bool = False)
|
|||
return is_source_variable_name
|
||||
|
||||
|
||||
class StringFormatterValidator(Validator):
|
||||
class StringFormatterValidator(StringValidator):
|
||||
"""
|
||||
String that can use
|
||||
:class:`source variables <ytdl_sub.entries.variables.entry_variables.SourceVariables>`
|
||||
|
|
@ -73,7 +73,6 @@ class StringFormatterValidator(Validator):
|
|||
and would resolve to something like ``sweet_tv_show.s2022.e502.mp4``.
|
||||
"""
|
||||
|
||||
_expected_value_type = str
|
||||
_expected_value_type_name = "format string"
|
||||
_variable_not_found_error_msg_formatter = (
|
||||
"Format variable '{variable_name}' does not exist. Available variables: {available_fields}"
|
||||
|
|
|
|||
|
|
@ -138,6 +138,12 @@ class StringValidator(ValueValidator[str]):
|
|||
_expected_value_type = str
|
||||
_expected_value_type_name = "string"
|
||||
|
||||
def __init__(self, name: str, value: Any):
|
||||
if isinstance(value, (int, float, bool)):
|
||||
value = str(value)
|
||||
|
||||
super().__init__(name, value)
|
||||
|
||||
|
||||
class FloatValidator(ValueValidator[float]):
|
||||
_expected_value_type = (int, float)
|
||||
|
|
|
|||
|
|
@ -4,12 +4,12 @@ from ytdl_sub.utils.exceptions import ValidationException
|
|||
from ytdl_sub.validators.validators import StringValidator
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", ["a", "unicode 💩", ""])
|
||||
@pytest.mark.parametrize("value", ["a", "unicode 💩", "", 1, 3.14, True, False])
|
||||
def test_string_validator(value):
|
||||
string_validator = StringValidator(name="good_str_validator", value=value)
|
||||
assert string_validator._name == "good_str_validator"
|
||||
assert string_validator._value == value
|
||||
assert string_validator.value == value
|
||||
assert string_validator._value == str(value)
|
||||
assert string_validator.value == str(value)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", [None, {}, True, 0])
|
||||
|
|
|
|||
Loading…
Reference in a new issue