Ниже приведен метод, при котором я обрабатываю несколько пользовательских проверок с помощью пользовательских сообщений.
def social_login_validation(data):
"""Login validation schema."""
schema = Schema({
Required('first_name'):All(str, Length(min=1, max=30)),
Required('last_name'):All(str, Length(min=1, max=30)),
Optional('phone_number'):All(str, Length(min=10, max=10), validate_phone_number),
Optional('profile_picture'):Any(str, Length(min=1, max=255)),
Optional('email'):All(str, Length(min=10, max=255), validate_email),
Optional('auth_token'):Any(str, Length(min=10, max=255)),
Optional('auth_token_expiry_date'):Any(str, Length(min=10, max=255)),
Required('social_media_login_type'):All(str, Length(min=1, max=255),\
validate_social_media_login_type),
Required('social_id'):All(str, Length(min=10, max=255))
})
try:
schema(data)
except MultipleInvalid as error:
if "length" in str(error) and "['first_name']" in str(error) and "at most" in str(error):
message = RESPONSES['LENGTH_FIRST_NAME']
code = RESPONSES_CODE['BAD_REQUEST']
elif "length" in str(error) and "['last_name']" in str(error) and "at most" in str(error):
message = RESPONSES['LENGTH_LAST_NAME']
code = RESPONSES_CODE['BAD_REQUEST']
elif "length" in str(error) and "['social_media_login_type']"\
in str(error) and "at most" in str(error):
message = RESPONSES['LENGTH_LOGIN_TYPE']
code = RESPONSES_CODE['BAD_REQUEST']
elif "length" in str(error) and "['social_id']" in str(error) and "at most" in str(error):
message = RESPONSES['LENGTH_SOCIAL_ID']
code = RESPONSES_CODE['BAD_REQUEST']
elif "length" in str(error) and "['phone_number']" in str(error):
message = RESPONSES['LENGTH_PHONE_NUMBER']
code = RESPONSES_CODE['BAD_REQUEST']
elif "expected str" in str(error) and "['email']" in str(error):
message = RESPONSES['TYPE_EMAIL']
code = RESPONSES_CODE['BAD_REQUEST']
elif "data['first_name']" in str(error):
message = RESPONSES['EMPTY_FIRST_NAME']
code = RESPONSES_CODE['BAD_REQUEST']
elif "data['last_name']" in str(error):
message = RESPONSES['EMPTY_LAST_NAME']
code = RESPONSES_CODE['BAD_REQUEST']
elif "data['social_media_login_type']" in str(error):
message = RESPONSES['EMPTY_LOGIN_TYPE']
code = RESPONSES_CODE['BAD_REQUEST']
elif "data['social_id']" in str(error):
message = RESPONSES['EMPTY_SOCIAL_ID']
code = RESPONSES_CODE['BAD_REQUEST']
else:
return jsonify({'message':str(error.error_message)}), 400
return jsonify({'message':message}), code
Как лучше обрабатывать эти многочисленные пользовательские сообщения?