Unverified Commit 92dfa466 authored by mvdbeek's avatar mvdbeek
Browse files

Fix None passed to LengthValidator

It is valid to pass None as a default value to the validator. It should
just return ValueError, which it will do with this change via the inner
validator.

Fixes https://github.com/galaxyproject/galaxy/issues/17961

Ideally we'd add type annotations, but with the current structure and
instantiation order it seems impossible to do this in a meaningful way.

If we refactored the validators to be functions, and validators
functions were registered per parameter type we could have narrow types on the
validator signature.
parent 32327ae4
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -186,7 +186,9 @@ class LengthValidator(InRangeValidator):
        super().__init__(message, range_min=length_min, range_max=length_max, negate=negate)

    def validate(self, value, trans=None):
        super().validate(len(value), trans)
        if value is None:
            raise ValueError("No value provided")
        super().validate(len(value) if value else 0, trans)


class DatasetOkValidator(Validator):
+8 −0
Original line number Diff line number Diff line
@@ -186,6 +186,14 @@ class TestParameterValidation(BaseParameterTestCase):
            p.validate("bar")
        p.validate("f")
        p.validate("foobarbaz")
        p = self._parameter_for(
            xml="""
<param name="blah" type="text" optional="false">
    <validator type="length" min="2" max="8"/>
</param>"""
        )
        with self.assertRaisesRegex(ValueError, "No value provided"):
            p.validate(None)

    def test_InRangeValidator(self):
        p = self._parameter_for(