Skip to content

Commit

Permalink
Support optional args in typer validator factory
Browse files Browse the repository at this point in the history
  • Loading branch information
JimMadge committed Nov 29, 2023
1 parent ba05dd7 commit e1a0264
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions data_safe_haven/functions/typer_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@


def typer_validator_factory(validator: Callable[[Any], Any]) -> Callable[[Any], Any]:
"""Factory to create validation functions for Typer from Pydantic validators"""
def typer_validator(x: Any) -> Any:
# Return unused optional arguments
if x is None:
return x

# Validate input, catching ValueError to raise Typer Exception
try:
validator(x)
return x
Expand Down
3 changes: 3 additions & 0 deletions tests_/functions/test_typer_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ def test_typer_validate_aad_guid_fail(self, guid):
with pytest.raises(BadParameter) as exc:
typer_validate_aad_guid(guid)
assert "Expected GUID" in exc

def test_typer_validate_aad_guid_nonae(self):
assert typer_validate_aad_guid(None) is None

0 comments on commit e1a0264

Please sign in to comment.