diff --git a/CHANGELOG.md b/CHANGELOG.md index 56125be5..0e8c94ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ ### Bug Fixes +* Serverless input constraints are enforced for supplied values even when the schema defines a default. * [BREAKING] `rp login` asks you to paste the API key shown in the console after approval. It no longer collects the key by polling the auth request, and it fails fast without an interactive terminal. * Task calls support indefinite execution with bounded interruption cleanup. Failed deletion retains the pod ID for recovery. * Network volume creation intersects catalog storage support with every sharing resource's hardware stock and datacenter constraints. Unsupported explicit pins fail before creation; existing volumes retain their authoritative IDs and datacenters. diff --git a/runpod/serverless/utils/rp_validator.py b/runpod/serverless/utils/rp_validator.py index 5a66e18e..2f77c768 100644 --- a/runpod/serverless/utils/rp_validator.py +++ b/runpod/serverless/utils/rp_validator.py @@ -68,21 +68,31 @@ def _validate_input_against_schema(schema, validated_input, error_list): except TypeError: continue - # skip type check assuming rare case, default value sets value - if "default" in rules and isinstance(validated_input[key], type(rules["default"])): - continue + matches_default = "default" in rules and isinstance( + validated_input[key], type(rules["default"]) + ) # Check for the correct type. is_instance = isinstance(validated_input[key], rules["type"]) - if not is_instance: + if not matches_default and not is_instance: _add_error( 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 + # Values accepted by matching the default's type are not passed to + # constraints. + if not is_instance: + 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..c8458c2c 100644 --- a/tests/test_serverless/test_utils/test_validate.py +++ b/tests/test_serverless/test_utils/test_validate.py @@ -92,6 +92,121 @@ 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_constraints_apply_when_default_is_set(self): + """ + Constraints are enforced on an optional input that has a default + """ + schema = { + "steps": { + "type": int, + "required": False, + "default": 10, + "constraints": lambda x: 0 < x <= 100, + } + } + + self.assertEqual( + rp_validator.validate({"steps": 5000}, schema), + {"errors": ["steps does not meet the constraints."]}, + ) + self.assertEqual( + rp_validator.validate({"steps": 50}, schema), + {"validated_input": {"steps": 50}}, + ) + self.assertEqual( + rp_validator.validate({}, schema), {"validated_input": {"steps": 10}} + ) + + def test_validate_default_of_other_type_skips_constraints(self): + """ + A None default is accepted and is not passed to the constraints + """ + schema = { + "prompt": { + "type": str, + "required": False, + "default": None, + "constraints": lambda x: len(x) < 5, + } + } + + for raw_input in ({}, {"prompt": None}): + with self.subTest(raw_input=raw_input): + self.assertEqual( + rp_validator.validate(raw_input, schema), + {"validated_input": {"prompt": None}}, + ) + self.assertEqual( + rp_validator.validate({"prompt": "too long"}, schema), + {"errors": ["prompt does not meet the constraints."]}, + ) + def test_validate_rules_not_dict(self): """ Tests validate with rules not dict