예제 #1
0
def errors_from_validationerror(
        validation_error: ValidationError) -> Sequence[str]:
    """Extract errors from a marshmallow ValidationError into a displayable format."""
    normalized_errors = validation_error.normalized_messages()

    # As of webargs 6.0, errors are inside a nested dict, where the first level should
    # always be a single-item dict with the key representing the "location" of the data
    # (e.g. query, form, etc.) - Check if the errors seem to be in that format, and if
    # they are, just remove that level since we don't care about it
    first_value = list(normalized_errors.values())[0]
    if isinstance(first_value, dict):
        errors_by_field = first_value
    else:
        # not a webargs error, so just use the original without any unnesting
        errors_by_field = normalized_errors

    error_strings = []
    for field, errors in errors_by_field.items():
        joined_errors = " ".join(errors)
        if field != "_schema":
            error_strings.append(f"{field}: {joined_errors}")
        else:
            error_strings.append(joined_errors)

    return error_strings
예제 #2
0
def errors_from_validationerror(
        validation_error: ValidationError) -> Sequence[str]:
    """Extract errors from a marshmallow ValidationError into a displayable format."""
    errors_by_field = validation_error.normalized_messages()

    error_strings = []
    for field, errors in errors_by_field.items():
        joined_errors = " ".join(errors)
        if field != "_schema":
            error_strings.append(f"{field}: {joined_errors}")
        else:
            error_strings.append(joined_errors)

    return error_strings
예제 #3
0
def validation_error_exception_message(e: ValidationError) -> str:
    validation_messages = e.normalized_messages()
    messages_list = []
    message_format = 'on field "%s" with message%s %s'
    messages_count = 0
    for field in sorted(validation_messages.keys()):
        messages = validation_messages[field]
        messages_count += len(messages)
        messages_str = ", ".join('"' + msg + '"' for msg in messages)
        message = message_format % (
            field,
            "" if len(messages) <= 1 else "s",
            messages_str,
        )
        messages_list.append(message)
    message = "Validation error%s: %s" % (
        "" if messages_count <= 1 else "s",
        "; ".join(messages_list),
    )
    return message
예제 #4
0
def collect_errors(error: ValidationError) -> str:
    parts = _collect_errors(error.normalized_messages(), [])
    return '\n'.join(parts)
예제 #5
0
def return_field_and_error(error: ValidationError) -> Tuple[str, str]:
    dict_error = error.normalized_messages()
    key, value = get_last_key_value(nested_dict=dict_error).__next__()
    return key, value
def _handle_validation_error(e: ValidationError) -> Response:
    resp = jsonify({'invalidFields': e.normalized_messages()})
    resp.status_code = 400
    return resp
예제 #7
0
def handle_marshmallow_ValidationError(
    exception: ValidationError, ) -> JSONResponse:
    return JSONResponse(
        exception.normalized_messages(),
        HTTPStatus.BAD_REQUEST,
    )
예제 #8
0
def handle_marshmallo_ValidationError(exception: ValidationError) -> Response:
    return Response(
        json.dumps(exception.normalized_messages()),
        HTTPStatus.BAD_REQUEST,
        headers={'Content-Type': 'application/json'},
    )
예제 #9
0
 def _get_message_from_error(error: mm.ValidationError) -> str:
     return [val[0] for val in error.normalized_messages().values()][0]
예제 #10
0
async def invalid_data(e: ma.ValidationError):
    return {
        "error": "invalid request",
        "extra": e.normalized_messages()
    }, 400