A ban could be brought in by April 2027 and the latest date for its introduction would be April 2030.

It was backed by animal welfare groups which said thousands of dogs in the UK have died in recent years due to racing.

Owen Sharp, from the Dogs Trust, said it was a “landmark moment” for dog welfare.

There is only one track in Wales, in Ystrad Mynach, near Caerphilly.

The GBGB took the Welsh government to court, claiming there hadn’t been enough consultation.

In their ruling, Lord Justice Lewis and Mr Justice Chamberlain, said: “Given that the bill is now validly before the Senedd, and especially in circumstances where it has completed its legislative stages, the determination of a claim alleging a procedural flaw at the pre-legislative stage would, in our judgment, be an impermissible interference with the proceedings of the Senedd and contrary to the constitutional separation of powers.”

Mark Bird, from GBGB, said the decision “appears to come down to arguments of a purely academic nature”.

“This Bill process was an unmerited attack on a welfare-driven, well-regulated sport in Wales and will only destroy family-run businesses and desperately needed jobs in service of animal rights activists,” he said.

He added the organisation was considering its options and “hope at the very least that the next government in Cardiff Bay do the right thing and repeal this flawed legislation” following May’s Senedd election.

The Cut the Chase coalition, a group made up of Blue Cross, Dogs Trust, Greyhound Rescue Wales, Hope Rescue, and RSPCA Cymru, welcomed the ruling.

“Welsh government action followed tens of thousands of people signing a petition supporting the phasing out of greyhound racing,” it said.

“A clear majority of Senedd Members then voted in favour of the legislation this week, and clearly this judgment is the right result.”