Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derek furst/constraints #711

Merged
merged 2 commits into from
Aug 2, 2024
Merged
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
32 changes: 32 additions & 0 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2294,6 +2294,9 @@ 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)
is_match = request.values.get('match')
order = request.values.get('order')

Expand Down Expand Up @@ -4343,6 +4346,35 @@ 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
Loading