Skip to content

Commit

Permalink
refactored constraints_json_is_valid into metadata_constraints and re…
Browse files Browse the repository at this point in the history
…moved extraneous index value
  • Loading branch information
DerekFurstPitt committed Sep 5, 2024
1 parent 8df6558 commit c2bf51c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
36 changes: 2 additions & 34 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2316,9 +2316,8 @@ def validate_constraints():
if not request.is_json:
bad_request_error("A json body and appropriate Content-Type header are required")
json_entry = request.get_json()
is_valid = constraints_json_is_valid(json_entry)
if is_valid is not True:
bad_request_error(is_valid)
if not isinstance(json_entry, list):
return "JSON body expects a list."
is_match = request.values.get('match')
order = request.values.get('order')

Expand All @@ -2329,9 +2328,7 @@ def validate_constraints():
'name': "ok"
}

index = 0
for constraint in json_entry:
index += 1
if order == 'descendants':
result = get_constraints(constraint, 'descendants', 'ancestors', is_match)
else:
Expand Down Expand Up @@ -4599,35 +4596,6 @@ def internal_server_error(err_msg):
abort(500, description = err_msg)


"""
Validates the incoming json for the endpoint /constraints.
Returns true if the json matches the required format. If
invalid, returns a string explaining why.
"""
def constraints_json_is_valid(json_entry):
if not isinstance(json_entry, list):
return "JSON body expects a list."

for constraint in json_entry:
if not isinstance(constraint, dict):
return "Each constraint in the list must be a JSON object."

for key in constraint:
if key not in ["ancestors", "descendants"]:
return f"Invalid key '{key}'. Allowed keys are 'ancestors' and 'descendants'."

value = constraint[key]
if isinstance(value, dict):
continue
elif isinstance(value, list):
for item in value:
if not isinstance(item, dict):
return f"The value for '{key}' must be represented as a JSON object or as a list of objects"
else:
return f"The value for '{key}' must be a JSON object or a list of JSON objects."
return True


"""
Parse the token from Authorization header
Expand Down
32 changes: 32 additions & 0 deletions src/metadata_constraints.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from deepdiff import DeepDiff
from flask import abort

def bad_request_error(err_msg):
abort(400, description = err_msg)

def build_constraint(ancestor: dict, descendants: list[dict]) -> dict:
return {
Expand Down Expand Up @@ -151,9 +155,37 @@ def validate_exclusions(entry, constraint, key) -> bool:
return True
else:
return False


"""
Validates the incoming json for the endpoint /constraints.
Returns true if the json matches the required format. If
invalid, returns a string explaining why.
"""
def constraints_json_is_valid(entry):
if not isinstance(entry, dict):
return "Each constraint in the list must be a JSON object."

for key in entry:
if key not in ["ancestors", "descendants"]:
return f"Invalid key '{key}'. Allowed keys are 'ancestors' and 'descendants'."

value = entry[key]
if isinstance(value, dict):
continue
elif isinstance(value, list):
for item in value:
if not isinstance(item, dict):
return f"The value for '{key}' must be represented as a JSON object or as a list of objects"
else:
return f"The value for '{key}' must be a JSON object or a list of JSON objects."
return True


def get_constraints(entry, key1, key2, is_match=False) -> dict:
is_valid = constraints_json_is_valid(entry)
if is_valid is not True:
bad_request_error(is_valid)
entry_key1 = get_constraint_unit(entry.get(key1))
msg = f"Missing `{key1}` in request. Use orders=ancestors|descendants request param to specify. Default: ancestors"
result = {'code': 400, 'name': "Bad Request"} if is_match else {'code': 200, 'name': 'OK', 'description': 'Nothing to validate.'}
Expand Down

0 comments on commit c2bf51c

Please sign in to comment.