Skip to content

Commit

Permalink
Refactor validation function in handlers.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Wh1isper committed Nov 19, 2023
1 parent 994d2b7 commit 01be932
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions jupyter_server/services/contents/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@
AUTH_RESOURCE = "contents"


def _validate_in_or_not(expect_in_model: bool, model: Dict[str, Any], maybe_none_keys: List[str]):
"""
Validate that the keys in maybe_none_keys are None or not None
"""

if expect_in_model:
errors = [key for key in maybe_none_keys if model[key] is None]
if errors:
raise web.HTTPError(
500,
f"Keys unexpectedly None: {errors}",
)
else:
errors = {key: model[key] for key in maybe_none_keys if model[key] is not None} # type: ignore[assignment]
if errors:
raise web.HTTPError(
500,
f"Keys unexpectedly not None: {errors}",
)


def validate_model(model, expect_content, expect_md5):
"""
Validate a model returned by a ContentsManager method.
Expand Down Expand Up @@ -51,30 +72,10 @@ def validate_model(model, expect_content, expect_md5):
f"Missing Model Keys: {missing}",
)

def validate_in_or_not(statement: bool, model: Dict[str, Any], maybe_none_keys: List[str]):
"""
Validate that the keys in maybe_none_keys are None or not None
"""

if statement:
errors = [key for key in maybe_none_keys if model[key] is None]
if errors:
raise web.HTTPError(
500,
f"Keys unexpectedly None: {errors}",
)
else:
errors = {key: model[key] for key in maybe_none_keys if model[key] is not None} # type: ignore[assignment]
if errors:
raise web.HTTPError(
500,
f"Keys unexpectedly not None: {errors}",
)

content_keys = ["content", "format"]
md5_keys = ["md5"]
validate_in_or_not(expect_content, model, content_keys)
validate_in_or_not(expect_md5, model, md5_keys)
_validate_in_or_not(expect_content, model, content_keys)
_validate_in_or_not(expect_md5, model, md5_keys)


class ContentsAPIHandler(APIHandler):
Expand Down

0 comments on commit 01be932

Please sign in to comment.