mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
Check that the request payload data is valid json.
By adding `force=True` to request.get_json() the mime type is ignore. If the data is not valid json the method will return a `BadRequestError` we catch that and throw our own error with a clear error message "Invalid JSON supplied in POST data". If the json is valid return the json data or an empty dict if None is passed in. This PR improves the error messages if the json is invalid, previously, the error message was "None object type" message which is not very helpful.
This commit is contained in:
@@ -7,8 +7,6 @@ from jsonschema import (Draft7Validator, ValidationError, FormatChecker)
|
||||
from notifications_utils.recipients import (validate_phone_number, validate_email_address, InvalidPhoneError,
|
||||
InvalidEmailError)
|
||||
|
||||
from app.v2.errors import BadRequestError
|
||||
|
||||
format_checker = FormatChecker()
|
||||
|
||||
|
||||
@@ -57,9 +55,6 @@ def validate_schema_date_with_hour(instance):
|
||||
|
||||
|
||||
def validate(json_to_validate, schema):
|
||||
if json_to_validate is None:
|
||||
raise BadRequestError(message="Request body is empty.",
|
||||
status_code=400)
|
||||
validator = Draft7Validator(schema, format_checker=format_checker)
|
||||
errors = list(validator.iter_errors(json_to_validate))
|
||||
if errors.__len__() > 0:
|
||||
|
||||
Reference in New Issue
Block a user