def respond_with_unprocessable(errors, status = :unprocessable_entity)
    render_errors(errors, status)
  end

  def error_detail(error_message)
    return error_message if error_message.is_a?(String)
    Hash[error_message.first, error_message.last]
  end

  def render_errors(errors, status)
    errors = errors.map do |attribute, error_messages|
      error_messages.map do |error_message|
        {
          attribute: attribute,
          detail_key: error_detail(error_message)
        }
      end
    end.flatten

    result = {
      data: {},
      errors: errors
    }

    render json: result, status: status
  end