Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions runpod/serverless/utils/rp_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
66 changes: 66 additions & 0 deletions tests/test_serverless/test_utils/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,72 @@ def test_validate_invalid_type(self):
"x should be <class 'int'> type, not <class 'str'>.", 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 <class 'int'> 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
Expand Down