diff --git a/README.md b/README.md index 91c3740a..7bcf0daf 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![PyPI Package](https://badge.fury.io/py/runpod.svg)](https://badge.fury.io/py/runpod) [![Downloads](https://static.pepy.tech/personalized-badge/runpod?period=total&units=international_system&left_color=grey&right_color=blue&left_text=downloads)](https://pepy.tech/project/runpod) -[![CI | Unit Tests](https://github.com/runpod/runpod-python/actions/workflows/CI-pytests.yml/badge.svg)](https://github.com/runpod/runpod-python/actions/workflows/CI-pytests.yml) +[![CI | Unit Tests](https://github.com/runpod/runpod-python/actions/workflows/ci.yml/badge.svg)](https://github.com/runpod/runpod-python/actions/workflows/ci.yml) [![License](https://img.shields.io/badge/License-MIT-blue)](LICENSE) [Documentation](https://docs.runpod.io) • [Examples](examples) • [Discord](https://discord.gg/pJ3P2DbUUq) diff --git a/runpod/serverless/utils/rp_validator.py b/runpod/serverless/utils/rp_validator.py index 5a66e18e..2d7c1d70 100644 --- a/runpod/serverless/utils/rp_validator.py +++ b/runpod/serverless/utils/rp_validator.py @@ -79,10 +79,16 @@ def _validate_input_against_schema(schema, validated_input, error_list): error_list, f"{key} should be {rules['type']} type, not {type(validated_input[key])}.", ) + # Constraints are written for the declared type, so a value + # of another type is reported once and not passed to them. + continue - # Check lambda constraints. - if "constraints" in rules and not rules["constraints"]( - validated_input.get(key) + # Check lambda constraints. A missing input has no value to check and + # was already reported as required or as a schema error. + if ( + key in validated_input + and "constraints" in rules + and not rules["constraints"](validated_input[key]) ): _add_error(error_list, CONSTRAINTS_ERROR.format(key)) diff --git a/tests/test_serverless/test_utils/test_validate.py b/tests/test_serverless/test_utils/test_validate.py index 60385130..98aacc46 100644 --- a/tests/test_serverless/test_utils/test_validate.py +++ b/tests/test_serverless/test_utils/test_validate.py @@ -92,6 +92,72 @@ def test_validate_invalid_type(self): "x should be type, not .", result["errors"] ) + def test_validate_missing_required_input_with_constraints(self): + """ + A missing required input is reported without calling its constraints + """ + schema = { + "steps": {"type": int, "required": True, "constraints": lambda x: x > 0} + } + + result = rp_validator.validate({}, schema) + + self.assertEqual(result, {"errors": ["steps is a required input."]}) + + def test_validate_missing_default_with_constraints(self): + """ + An optional input with no default is reported without calling its constraints + """ + schema = { + "steps": {"type": int, "required": False, "constraints": lambda x: x > 0} + } + + result = rp_validator.validate({}, schema) + + self.assertEqual( + result, {"errors": ["Schema error, missing default value for steps."]} + ) + + def test_validate_invalid_type_with_constraints(self): + """ + A value of the wrong type is reported without calling the constraints + """ + for value in ("20", None, [20]): + with self.subTest(value=value): + constraints = Mock(side_effect=lambda x: x > 0) + schema = { + "steps": {"type": int, "required": True, "constraints": constraints} + } + + result = rp_validator.validate({"steps": value}, schema) + + self.assertEqual( + result, + { + "errors": [ + f"steps should be type, not {type(value)}." + ] + }, + ) + constraints.assert_not_called() + + def test_validate_constraints_receive_valid_value(self): + """ + Constraints still run on a value of the declared type + """ + schema = { + "steps": {"type": int, "required": True, "constraints": lambda x: x > 0} + } + + self.assertEqual( + rp_validator.validate({"steps": 20}, schema), + {"validated_input": {"steps": 20}}, + ) + self.assertEqual( + rp_validator.validate({"steps": 0}, schema), + {"errors": ["steps does not meet the constraints."]}, + ) + def test_validate_rules_not_dict(self): """ Tests validate with rules not dict