One subject left uncovered in Donald Trump’s first State of the Union address was his performance in the courts. In his first year in office, Trump has repeatedly violated the Constitution. Just as often, he has been sued for it. And, in virtually every case, the Constitution has won and Trump has lost. Thus far, the courts have proven to be an important safeguard of constitutional liberty. The record is extraordinary.

In the “Muslim ban” cases, multiple courts have invalidated all three versions of President Trump’s travel ban, enacted to make good on his campaign promise to prohibit Muslims from entering the country. After denouncing the judge who struck down his first ban as a “so-called judge,” Trump abandoned the first two versions of the ban. The third and most recent version, like the first two, has also been declared unlawful. It’s now headed to the Supreme Court.

The courts have also rejected the president’s ill-advised ban on transgender military service, which he announced via a tweet without even consulting the military, which had previously determined there was no problem with transgender persons serving. Two federal courts have preliminarily struck down the ban.

Federal courts in the District of Columbia have blocked the Trump administration’s efforts to bar teenagers in immigration custody from obtaining abortions. The head of the Office of Refugee Resettlement, Scott Lloyd, an ardent opponent of abortion, has refused to allow four undocumented minors to obtain abortions. Each time the ACLU has sued, and each time the women have been able to get the care they need.

The courts have also stood up for the “Dreamers,” undocumented immigrants whose parents brought them here as children. In December, a federal court ordered the administration to allow Dreamers to renew their applications for protection from deportation, under the program known as Deferred Action for Childhood Arrivals, or DACA.

Another federal court has insisted, over Trump administration objections, that a US citizen held in secret as an “enemy combatant” without charges must be given access to an attorney, and shall not be transferred without notice, to ensure that he has a right to challenge the legality of his detention.

And when it comes to health care, a federal court in Pennsylvania has preliminarily blocked President Trump’s rollback of an Obamacare requirement that employers cover the cost of contraception in their employees’ health-insurance plans. Another court barred the Trump administration from stripping federal funds from cities and counties that adopt “sanctuary” policies and decline to enforce federal immigration laws.

Lawsuits also brought an ignominious end to Trump’s “voter integrity” commission, headed by Kansas Secretary of State Kris Kobach and designed to establish a basis for supporting Republican voter-suppression efforts. Trump’s order closing down the commission explained that “rather than engage in endless legal battles at taxpayer expense, today I signed an executive order to dissolve the Commission.” The commission had been sued not only by numerous civil-rights groups, but even by one of its own members.

One constitutional challenge has failed. A federal judge in New York recently dismissed a case charging Trump with violating the Constitution’s emoluments clause, which forbids the president from accepting any “emolument,” or payment, from a foreign or domestic state official. The court did not rule on the merits, but concluded that the plaintiffs did not have sufficiently concrete injuries to raise the claim. Two other emoluments-clause lawsuits are pending, and in one of them, brought by the attorneys general of Maryland and the District of Columbia, the judge expressed disagreement with the New York court’s decision, signaling that Trump may see a loss there as well.

Why have courts been so willing to stand up to Trump? While the federal judiciary was designed to impose checks on officials who disregard constitutional constraints, the courts have not always lived up to their responsibility. One reason they may be doing so now is that Trump is so contemptuous of constitutional law—and indeed, of courts themselves. The courts are defending not only constitutional rights but also their own role in the legal system.

But don’t underestimate the significance of another factor: public opposition to Trump’s initiatives. When the public marches in the streets, national-security officials and leading businesses join briefs challenging the president’s actions, and state attorneys general join the chorus of critics, the courts are much more likely to do their job. Popular opposition means that judges do not stand alone.

The Supreme Court has yet to weigh in on any of these disputes. It will rule by the end of June on the current version of the travel ban. In the meantime, it has stayed the lower courts’ travel-ban injunctions pending its review—not an encouraging sign. We will soon know whether the Supreme Court will, like the lower courts, serve as a guardian of the rule of law against a president who sometimes seems not even to understand the concept.

Editor’s note: The opening of this article has been updated, to account for Trump’s State of the Union speech.