Skip to content

Commit

Permalink
Return MultipleInvalid instead of ValueError
Browse files Browse the repository at this point in the history
  • Loading branch information
ugexe committed Jun 5, 2024
1 parent 9b54e57 commit e9c7152
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 7 additions & 2 deletions minfraud/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from typing import Optional

from email_validator import validate_email # type: ignore
from voluptuous import All, Any, In, Match, Range, Required, Schema
from voluptuous import All, Any, In, Match, MultipleInvalid, Range, Required, Schema
from voluptuous.error import UrlInvalid

# Pylint doesn't like the private function type naming for the callable
Expand Down Expand Up @@ -401,7 +401,12 @@ def _transaction_id(s: Optional[str]) -> str:
def _validate_at_least_one_identifier_field(report):
optional_fields = ["ip_address", "maxmind_id", "minfraud_id", "transaction_id"]
if not any(field in report for field in optional_fields):
raise ValueError("The report must contain at least one of the following fields: 'ip_address', 'maxmind_id', 'minfraud_id', 'transaction_id'.")
# We return MultipleInvalid instead of ValueError to be consistent with what
# voluptuous returns.
raise MultipleInvalid(
"The report must contain at least one of the following fields: "
"'ip_address', 'maxmind_id', 'minfraud_id', 'transaction_id'."
)
return True


Expand Down
2 changes: 2 additions & 0 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ def test_tag(self):
self.check_invalid_report({"tag": bad})

def test_report_valid_identifier(self):
self.check_invalid_report_no_setup({"tag": "chargeback"})

self.check_report_no_setup({"tag": "chargeback", "ip_address": "1.1.1.1"})
self.check_report_no_setup(
{"tag": "chargeback", "minfraud_id": "58fa38d8-4b87-458b-a22b-f00eda1aa20d"}
Expand Down

0 comments on commit e9c7152

Please sign in to comment.