The Supreme Court’s rejection of the Trump administration’s effort to end protections against deportation for young immigrants wasn’t just a victory for the group known as “dreamers.” It was a triumph, and another one — as unexpected as it was welcome — for the rule of law.

The Trump administration has been an unending stress test for the American arrangement of checks and balances. Some aspects of the system have crashed, especially the willingness of Congress to provide meaningful restraint on a rogue executive. The revelations in former national security adviser John Bolton’s forthcoming book — about to be published notwithstanding the Trump Justice Department’s blatantly unconstitutional attempt to block it — offer just the latest example of congressional Republicans’ failure to perform their constitutional duties.

Not so the courts — at least so far. There is a conservative majority on the Supreme Court, and Trump is about to pass the mark of 200 judicial confirmations. As the Supreme Court finishes up its term, with important rulings still to come on abortion rights (will the court effectively abandon a decision from 2016, when Justice Anthony M. Kennedy was still on the court?), the intersection of religious freedom and employment discrimination, and the ability of lawmakers and prosecutors to subpoena the president’s financial records, it would be naive not to expect some disappointing results. Indeed, because those cases involve issues of constitutional law, not statutory interpretation or administrative law, progressive losses could be far more damaging.

And yet: Time after time, it is the courts, including the Supreme Court, that have presented the most effective bulwark against Trump’s excesses. The central figure here has been a surprising one: Chief Justice John G. Roberts Jr., a stalwart conservative.

This phenomenon was on display last year, when the court refused to blind itself to reality as the Trump administration tried to lie its way to adding a question about citizenship to the census. Roberts joined the court’s four liberal justices to reject the administration’s pretextual justifications for the move.

“Accepting contrived reasons would defeat the purpose of the enterprise,” Roberts wrote. “If judicial review is to be more than an empty ritual, it must demand something better than the explanation offered for the action taken in this case.”

It was on display earlier this week, when the court ruled that the federal law prohibiting employers from discriminating because of “sex” included protections from discrimination on the basis of sexual orientation and gender identity. Roberts, in a decision written by Justice Neil M. Gorsuch, Trump’s first Supreme Court nominee, aligned with liberals to find that the words of the statute meant what they said, even if its drafters never contemplated this result.

“Only the written word is the law,” Gorsuch wrote, “and all persons are entitled to its benefit.”

And it was on display Thursday, with the court’s ruling on the Trump administration’s effort to end the Deferred Action for Childhood Arrivals (DACA) program. As with the census case, the underlying legal issues involved arid questions of administrative law: Did courts even have the authority to review the decision? Yes, the majority said.

When the administration moved to revoke the program, did it provide an adequate explanation for its action? No. Rather, the justification was “arbitrary and capricious,” because the Department of Homeland Security failed to consider less drastic alternatives than complete dismantling, and failed to deal with the fact that thousands of dreamers had relied on the promise of being permitted to remain for some time.

Was the department allowed — nine months later — to come up with a more convincing rationale? No again. “The basic rule here is clear: An agency must defend its actions based on the reasons it gave when it acted,” Roberts said. “This is not the case for cutting corners to allow DHS to rely upon reasons absent from its original decision.”

Of course, this is the administration of cut corners. As Fred Barbash and Deanna Paul reported in The Post last year, “Federal judges have ruled against the Trump administration at least 63 times over the past two years, an extraordinary record of legal defeat that has stymied large parts of the president’s agenda on the environment, immigration and other matters.” Judges, they said, “have rebuked Trump officials for failing to follow the most basic rules of governance for shifting policy, including providing legitimate explanations supported by facts and, where required, public input.”

And, of course, in Trump’s cynical, solipsistic universe, law is merely politics by other means, and all outcomes are judged by how they relate to him.

“These horrible & politically charged decisions coming out of the Supreme Court are shotgun blasts into the face of people that are proud to call themselves Republicans or Conservatives,” he thundered on Twitter. And then, “Do you get the impression that the Supreme Court doesn’t like me?”

We remain, Trump notwithstanding, a society governed by rules and law. The dry terms of the Administrative Procedure Act speak in the language of preventing “arbitrary and capricious” action in agency rulemaking, but the import of those words goes beyond the narrow bounds of administrative law. This is an arbitrary and capricious presidency, yet there remain structures capable of containing its abuses — and individuals empowered and willing to do so.

Read more: