Generally speaking, you want to have pipes and cables as close to the centre of the wall as possible.
If it is necessary to have something pass close to the edge of a stud, a protection plate should be installed on the surface of the stud. A protection plate is a piece of sheet steel, probably about 16-18 ga that prevents a screw or nail from being driven into the stud where the shallow-located cable or pipe is.
You can usually purchase protection plates in the electrical department of a home improvement store. As a quick alternative, if you have a removed side from an electrical box that has been ganged with another box, you could use it, though you might have to pound it flat first.