繁体   English   中英

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

[英]What is proper way to handle custom validation in 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

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

看来您使用的是voluptuous包装?

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

如果稍微看一下源代码 ,就会发现可以通过error.errors访问错误。 我会仔细研究一下,看看是否可以以比将错误转换为字符串小的方式改变错误的方式来访问错误。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM