ALEXANDRIA, Va. (AP) — A federal judge has ruled that a northern Virginia sheriff was within his rights to fire a high-ranking deputy who supported an opposing candidate.

Deputy Mark McCaffrey sued Loudoun County Sheriff Mike Chapman after he opted not to reappoint McCaffrey when Chapman won re-election in 2015.

McCaffrey was fired after supporting Chapman's opponent in the GOP primary, Eric Noble.

In his Oct. 12 opinion, Judge Anthony Trenga said precedent in the Fourth Circuit makes clear that sheriffs can fire deputies who choose to support other candidates when the deputy holds a senior "policymaking" position. Trenga said McCaffrey, who served as lead detective on major cases including murders, qualifies as a senior policy maker.

McCaffrey has appealed the judge's ruling to the federal appeals court in Richmond.

__

The story has been updated to correct the sheriff's last name from Simpson to Chapman.