A general proof of the conservation of the curvature perturbation