在flask中处理自定义验证的正确方法是什么?

时间:2018-10-24 05:48:29

标签: python flask

以下是我使用自定义消息处理多个自定义验证的方法。

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

处理这些许多自定义消息的更好方法是什么?

1 个答案:

答案 0 :(得分:0)

您似乎正在使用voluptuous软件包?

您可能不想使用包的humanize功能,而不是自己编写每个错误消息。

如果稍微看一下source code,您会发现可以通过error.errors访问错误。我会仔细研究一下,看看是否可以以比将错误转换为字符串少的方法来解决错误。

我从未使用过此软件包,但请将其添加到问题标签中,以便人们知道他们在看什么。