def validate_jobfile_format(jobfile):
  """Takes a jobfile dictionary and returns it if formatted properly.

  Args:
    jobfile: dictionary after file ingestion

  Returns:
    jobfile dictionary

  Raises:
    ValueError
  """

  first_level_fields = [
      'job_name', 'job_type',
      'creative_source_type', 'creative_source_details',
      'data_destination', 'run_details'
  ]
  for attr in first_level_fields:
    if attr not in jobfile:
      msg = '{} missing from jobfile, refer to sample.yaml'.format(attr)
      raise ValueError(msg)

  job_types = ['image', 'video']
  if jobfile['job_type'] not in job_types:
    msg = 'job_type {} not recognized, must be one of {}'.format(
        jobfile['job_type'], job_types)
    raise ValueError(msg)

  creative_source_fields = {
      'bigquery': ['gcp_project', 'bq_dataset', 'bq_table'],
      'cm': ['cm_account_id', 'cm_profile_id', 'start_date', 'end_date'],
      'gcs': ['gcs_bucket', 'gcp_project']
  }
  if jobfile['creative_source_type'] not in creative_source_fields:
    msg = 'creative_source_type {} not recognized, must be one of {}'.format(
        jobfile['creative_source_type'], creative_source_fields.keys())
    raise ValueError(msg)
  for attr in creative_source_fields[jobfile['creative_source_type']]:
    if attr not in jobfile['creative_source_details']:
      msg = 'need {} to read from {}, refer to sample.yaml'.format(
          attr, jobfile['creative_source_type'])
      raise ValueError(msg)

  second_level_fields = {
      'data_destination': ['gcp_project', 'bq_dataset', 'gcs_bucket'],
      'run_details': ['gcp_project', 'temp_location', 'staging_location']
  }
  for field in second_level_fields:
    for attr in second_level_fields[field]:
      if attr not in jobfile[field]:
        msg = 'need {} in {}, refer to sample.yaml'.format(attr, field)
        raise ValueError(msg)

  return jobfile