Restrict certain roles registrations by domain

You’ll want to checkout the registration_errors filter. Make sure the role is being posted within the particular registration form.
Instead of conditionally adding your action you have to check within the filter if role and email are valid.

add_action( 'registration_errors', 'wpse_248123_registration_errors' );

function wpse_248123_registration_errors( $sanitized_user_login, $user_email, $errors ) {

  if ( empty( $_POST['role'] ) ) {
    $errors->add( 'empty_role', __( '<strong>ERROR</strong>: The role is invalid.' ) )
  }

  if ( 'ROLE1' === $_POST['role'] && !wpse_248123_is_valid_email_domain( $user_email ) ) {
    $errors->add( 'invalid_email_for_role', __( '<strong>ERROR</strong>: The email address isn&#8217;t allowed for this role.' ) )
  }

  return $errors;
}

function wpse_248123_is_valid_email_domain( $email="" ) {

  $email = (string) $email;
  $valid = false;

  if ( empty( $email ) ) {
    return $valid;
  }

  $valid_email_domains = array("gmail.com", "yahoo.com");// whitelist email domain lists

  foreach( $valid_email_domains as $d ) {

    $d_length = strlen( $d );
    $current_email_domain = strtolower( substr( $email, -($d_length), $d_length ) );

    if( $current_email_domain === strtolower($d) ){
      $valid = true;
      break;
    }

  }

  return $valid;

}