From 50634e86b9de5dc5494fa9953822cce0ca31c8a0 Mon Sep 17 00:00:00 2001 From: jbannon Date: Mon, 4 Apr 2022 05:21:37 +0000 Subject: [PATCH] select formatter, docstring update --- .../base/string_formatter_validator.py | 1 - .../base/string_select_validator.py | 22 +++++++++++++++++++ ytdl_subscribe/validators/base/validators.py | 6 +++-- 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 ytdl_subscribe/validators/base/string_select_validator.py diff --git a/ytdl_subscribe/validators/base/string_formatter_validator.py b/ytdl_subscribe/validators/base/string_formatter_validator.py index 31381e68..00ef3c52 100644 --- a/ytdl_subscribe/validators/base/string_formatter_validator.py +++ b/ytdl_subscribe/validators/base/string_formatter_validator.py @@ -10,7 +10,6 @@ class StringFormatterValidator(StringValidator): Ensures user-created formatter strings are valid """ - expected_value_type = str expected_value_type_name = "format string" FIELDS_VALIDATOR = re.compile(r"{([a-z_]+?)}") diff --git a/ytdl_subscribe/validators/base/string_select_validator.py b/ytdl_subscribe/validators/base/string_select_validator.py new file mode 100644 index 00000000..0e2e1a32 --- /dev/null +++ b/ytdl_subscribe/validators/base/string_select_validator.py @@ -0,0 +1,22 @@ +import re +from keyword import iskeyword +from typing import List +from typing import Set + +from ytdl_subscribe.validators.base.validators import StringValidator + + +class StringSelectValidator(StringValidator): + """ + Ensures strings have selected one of the discrete allowed values. + """ + + select_values: Set[str] = set() + + def __init__(self, name, format_string: str): + super().__init__(name=name, value=format_string) + + if self.value not in self.select_values: + raise self._validation_exception( + f"Must be one of the following values: {', '.join(self.select_values)}" + ) diff --git a/ytdl_subscribe/validators/base/validators.py b/ytdl_subscribe/validators/base/validators.py index 885c213b..cb1086dd 100644 --- a/ytdl_subscribe/validators/base/validators.py +++ b/ytdl_subscribe/validators/base/validators.py @@ -9,10 +9,12 @@ from ytdl_subscribe.validators.exceptions import ValidationException class Validator: """ - Abstract class used to validate any kind of field. Will ensure the value is the specified type. + Used to validate the value of a python object. This is the 'base' class that will first + check that the value's type matches the expected type. Validators that inherit from this should + perform their validation within the __init__. """ - # The python type that value should be + # If the value is not this expected type, error expected_value_type: Type = object # When raising an error, call the type this value instead of its python name