代码之家  ›  专栏  ›  技术社区  ›  krunal shah

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

  •  0
  • krunal shah  · 技术社区  · 7 年前

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

    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 回复  |  直到 7 年前
        1
  •  0
  •   Joost    7 年前

    看来你在用 voluptuous 包裹?

    您可能希望使用 humanize 软件包的功能。

    如果你看看 source code 有一点,你可以看到你可以通过 error.errors 是的。我将对此进行一些研究,看看您是否能够以比将错误转换为字符串更简单的方式访问错误。

    我从来没有用过这个包裹,但是请把它加到问号标签上,这样人们就知道他们在看什么了。