mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-31 23:26:23 -05:00
Make email_from and subject required attributes of the email_content schema.
Update the format_checkers to raise the specific exception that why the validator can handle multiple messages. Which led to a refactor of build_error_message.
This commit is contained in:
@@ -1,38 +1,45 @@
|
||||
import json
|
||||
|
||||
from jsonschema import (Draft4Validator, ValidationError, FormatChecker)
|
||||
from notifications_utils.recipients import (validate_phone_number, validate_email_address)
|
||||
from notifications_utils.recipients import (validate_phone_number, validate_email_address, InvalidPhoneError,
|
||||
InvalidEmailError)
|
||||
|
||||
|
||||
def validate(json_to_validate, schema):
|
||||
format_checker = FormatChecker()
|
||||
|
||||
@format_checker.checks('phone_number')
|
||||
@format_checker.checks('phone_number', raises=InvalidPhoneError)
|
||||
def validate_schema_phone_number(instance):
|
||||
return validate_phone_number(instance)
|
||||
validate_phone_number(instance)
|
||||
return True
|
||||
|
||||
@format_checker.checks('email_address')
|
||||
@format_checker.checks('email_address', raises=InvalidEmailError)
|
||||
def validate_schema_email_address(instance):
|
||||
return validate_email_address(instance)
|
||||
validate_email_address(instance)
|
||||
return True
|
||||
|
||||
validator = Draft4Validator(schema, format_checker=format_checker)
|
||||
errors = list(validator.iter_errors(json_to_validate))
|
||||
if errors.__len__() > 0:
|
||||
raise ValidationError(build_error_message(errors, schema))
|
||||
raise ValidationError(build_error_message(errors))
|
||||
return json_to_validate
|
||||
|
||||
|
||||
def build_error_message(errors, schema):
|
||||
def build_error_message(errors):
|
||||
fields = []
|
||||
for e in errors:
|
||||
field = "'{}' {}".format(e.path[0], e.schema.get('validationMessage')) if e.schema.get(
|
||||
'validationMessage') else e.message
|
||||
s = field.split("'")
|
||||
field = {"error": "ValidationError", "message": "{}{}".format(s[1], s[2])}
|
||||
fields.append(field)
|
||||
field = "{} {}".format(e.path[0], e.schema.get('validationMessage')) if e.schema.get(
|
||||
'validationMessage') else __format_message(e)
|
||||
fields.append({"error": "ValidationError", "message": field})
|
||||
message = {
|
||||
"status_code": 400,
|
||||
"errors": fields
|
||||
}
|
||||
|
||||
return json.dumps(message)
|
||||
|
||||
|
||||
def __format_message(e):
|
||||
s = e.message.split("'")
|
||||
msg = "{}{}".format(s[1], s[2])
|
||||
return msg if not e.cause else "'{}' {}".format(e.path[0], e.cause.message)
|
||||
|
||||
@@ -80,7 +80,7 @@ email_content = {
|
||||
"body": {"type": "string"},
|
||||
"subject": {"type": "string"}
|
||||
},
|
||||
"required": ["body"]
|
||||
"required": ["body", "from_email", "subject"]
|
||||
}
|
||||
|
||||
post_email_response = {
|
||||
|
||||
Reference in New Issue
Block a user