https://www.nato.int/cps/en/natohq/topics_110496.htm
Collective defence means that an attack against one Ally is considered as an attack against all Allies.
What prevents a member country to leave NATO to not have to go to war against a country that attacked and then join NATO again after the war is over. Is there a law that forbids countries from doing so explicitly, and if not why isn't it explicitly written in law?